Boost C++ Regex Example Compile Error - boost-regex

I'm having an extremely frustrating time trying to get the Boost regex library to behave itself in XCode 8.
I've finally managed to sort the includes out, now I'm hitting compiler errors when attempting to run the following regex example from Boost's library documentation here.
The code is as follows:
void print_captures(const std::string& regx, const std::string& text)
{
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if(boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i, j;
std::cout << "** Match found **\n Sub-Expressions:\n";
for(i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
std::cout << " Captures:\n";
for(i = 0; i < what.size(); ++i)
// compiler error in line above
{
std::cout << " $" << i << " = {";
for(j = 0; j < what.captures(i).size(); ++j)
// compiler erro in line above
{
if(j)
std::cout << ", ";
else
std::cout << " ";
std::cout << "\"" << what.captures(i)[j] << "\"";
}
std::cout << " }\n";
}
}
else
{
std::cout << "** No Match found **\n";
}
}
int main(int , char* [])
{
print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee");
print_captures("(.*)bar|(.*)bah", "abcbar");
print_captures("(.*)bar|(.*)bah", "abcbah");
print_captures("^(?:(\\w+)|(?>\\W+))*$",
"now is the time for all good men to come to the aid of the party");
return 0;
}
I'm getting two errors at the points indicated in the block above that both read:
No member named 'captures' in 'boost::match_results<std::__1::__wrap_iter<const char *>, std::__1::allocator<boost::sub_match<std::__1::__wrap_iter<const char *> > > >'
Apologies in advance if this question has been answered elsewhere.
Any idea how I can fix this?

Related

c++ Getting the Summation of numbers using an array and pointers

So I was doing a program where i need to find the total area of all floors, where the floors are determined by the user. I think I'm correct on the part to use the pointers, so to check if the code was correct I tried it doing it with basic addition. but even with basic addition it seems to have already some problems. I tried looking for similar questions in here and I can't find anything that might help me, so I hope you guys can help me. Thank you in advance.
float *Length=NULL, *Width=NULL, *Area=NULL, TotalArea, templ, tempw;
int floors, count;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
for (int loop = 0; loop < floors; loop++)
{
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length + loop) = templ;
*(Width + loop) = tempw;
*(Area + loop) = (*Length + loop) + (*Width + loop);
count = loop;
for (int count = 0; count < floors; count++)
{
TotalArea = TotalArea + *Area+count;
}
}
cout << TotalArea << endl;
I tried inputting the following:
floor:2
floor 1
length: 1
width: 1
floor 2
length: 1
width: 1
The answer should be 4, but the output ends up with 10.
You don't need arrays and pointers. Since you are looping through the floors and summing the areas of each floor, you can "forget" about previously encountered lengths, widths and areas, and only remember the total area encountered so far.
float length;
float width;
float area;
float totalArea;
int floors;
std::cout << "Input the number of floors to proceed\n";
std::cout << ":";
std::cin >> floors;
totalArea = 0;
for (int loop = 0; loop < floors; loop++)
{
std::cout << "\n\nFloor " << loop + 1 << std::endl;
std::cout << "Input the Length: " << std::endl;
std::cin >> length;
std::cout << "Input the Width: " << std::endl;
std::cin >> width;
area = length * width;
totalArea += area;
}
std::cout << totalArea << std::endl;
Notice how I added these annoying std:: everywhere? If you wonder why, see this other question: Why is “using namespace std;” considered bad practice?
I tried making some changes based on you guys comments and here is what I did.
There are some warnings I don't really know why but It seems to be working
float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;
int floors, loop = 0;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
for (int loop = 0; loop < floors; loop++)
{
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length +loop) = l;
*(Width+loop) = w;
Area[floors] = (*(Length+loop)) * (*(Width+loop));
TotalArea += a[floors];
}
cout << TotalArea << endl;
This is what I came u with, it seems to be working, the output is also correct now.

Passing by reference character arrays

I'm attempting to pass a character array located in _name into a char reference called name.
However, when passing the array pointer into the reference, it would ONLY display the first character rather than the whole string.
My question is how would you create a Character reference array to copy the original pointer into it then displaying it? As show in item.cpp we copy _name pointer into reference of name then return name, it however only displays the first character of the string.
I will only show the relevant pieces of my code.
Item.cpp:
void Item::name(const char * name){
strncpy(_name, name , 20);
}
const char& Item::name() const{
char& name = *_name;
return name;
}
ItemTester.cpp:
Main():
int main(){
double res, val = 0.0;
fstream F;
SItem Empty;
SItem A("456", "AItem", 200);
SItem B("567", "BItem", 300, false);
//cout << A.name() << endl;
B.quantity(50);
//cout << Empty << endl;
cout << A << endl << B << endl << endl;
cout << "Enter Item info for A: (Enter 123 for sku)" << endl;
cin >> A;
cout << "Copying A in C ----" << endl;
SItem C = A;
cout << C << endl << endl;
cout << "Saving A---------" << endl;
A.save(F);
cout << "Loading B----------" << endl;
B.load(F);
cout << "A: ----------" << endl;
cout << A << endl << endl;
cout << "B: ----------" << endl;
cout << B << endl << endl;
cout << "C=B; op=----------" << endl;
C = B;
cout << C << endl << endl;;
cout << "Operator ==----------" << endl;
cout << "op== is " << ((A == "123") && !(A == "234") ? "OK" : "NOT OK") << endl << endl;
cout << "op+=: A += 20----------" << endl;
A += 20;
cout << A << endl << endl;
cout << "op-=: A -= 10----------" << endl;
A -= 10;
cout << A << endl << endl;
cout << "op+=double: ----------" << endl;
res = val += A;
cout << res << "=" << val << endl << endl;
return 0;
}
ostream write
virtual std::ostream& write(std::ostream& os, bool linear)const{
return os << sku() << ": " << name() << endl
<< "Qty: " << quantity() << endl
<< "Cost (price * tax): " << fixed << setprecision(2) << cost();
}
Let me know if i missed any important details and il edit my post with it.
char& is reference to char, thus just a single character. Reference to array of characters would be char*&.
Example:
class Test
{
private:
static const size_t maxlen = 100;
char* _name;
public:
Test() : _name(new char[maxlen+1]) {}
~Test() {delete _name;}
void name(const char* s)
{
if(strlen(s) >= maxlen)
throw "too long";
else
{
memcpy(_name, s, strlen(s) * sizeof(char));
_name[strlen(s)] = '\0';
}
}
char*& name()
{
return _name;
}
};
int main()
{
Test obj;
obj.name("testname");
cout<<"Name = "<<obj.name()<<endl;
obj.name()[0] = '*';
cout<<"After change: Name = "<<obj.name()<<endl;
return 0;
}
EDIT:
I would change "getter" to something like:
char*& Item::name() {
return _name;
}
Actually if you do want the method to be "const", in the sense that user of the class should not change the elements of the array, or the actual address of the array, then you need not return a char*&, you can simply return const char*
const char* Item::name() const {
return _name;
}
As far as I see, the purpose of a char*& type is that the client would be able to change the actual address of an address.
As CForPhone pointed out, char& is not really what you want, you probably meant char*. But even then, using char* to represent strings is for C. In C++, you should use std::string:
const string Item::name() const{
string name(_name);
return name;
}

How can I translate this C++ function to C?

The code below is in C++. How do I translate it to C?
void drawBoard()
{
system( "cls" );
cout << "SCORE: " << score << endl << endl;
for( int y = 0; y < 4; y++ )
{
cout << "+------+------+------+------+" << endl << "| ";
for( int x = 0; x < 4; x++ )
{
if( !board[x][y].val ) cout << setw( 4 ) << " ";
else cout << setw( 4 ) << board[x][y].val;
cout << " | ";
}
cout << endl;
}
cout << "+------+------+------+------+" << endl << endl;
}
The code is pretty much C compatible already. However, the couts are a C++ construct.
To make it fully C compatible, you could replace cout with printf. For instance, in your code,
cout << "SCORE: " << score << endl << endl; --> printf("SCORE: %d \n\n", score);
You'll have to play around with the different parameters to get the formatting and output right, but that's the general idea. A good reference is this site: Printf
Assuming val and score are int, the following lines:
cout << "SCORE: " << score << endl << endl;
cout << "+------+------+------+------+" << endl << "| ";
cout << setw( 4 ) << " ";
cout << setw( 4 ) << board[x][y].val;
would translate to:
printf("SCORE: %d\n\n", score);
printf("+------+------+------+------+\n| ";
printf(" ");
printf("%4d", board[x][y].val);
You can figure out the rest .

CUSP library called from Fortran not working

I want to repetitively solve the CG/BicGSTAB using CUSP solver, called from Fortran. To avoid transfers I am passing the Fortran data directly to CUSP. The code compiles but breaks at the run time flagging:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
terminate called recursively
Aborted (core dumped)
Let alone the core of the code, even the print stream is not happening. The code of course is in the preliminary stage, but I wonder what is wrong with it.
extern "C" void bicgstab_(int *device_I, int *device_J, float *device_V, float *device_x, float *device_b, int *n, int *nnz){
int N = *n;
int NNZ = *nnz;
std::cout << N << " " << NNZ << " " << *device_I << std::endl;
for(int i=0; i<N;i++)std::cout << device_I[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_J[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_V[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_x[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_b[i] << " "; std::cout << std::endl;
// *NOTE* raw pointers must be wrapped with thrust::device_ptr!
thrust::device_ptr<int> wrapped_device_I(device_I);
thrust::device_ptr<int> wrapped_device_J(device_J);
thrust::device_ptr<float> wrapped_device_V(device_V);
thrust::device_ptr<float> wrapped_device_x(device_x);
thrust::device_ptr<float> wrapped_device_b(device_b);
// use array1d_view to wrap the individual arrays
typedef typename cusp::array1d_view< thrust::device_ptr<int> > DeviceIndexArrayView;
typedef typename cusp::array1d_view< thrust::device_ptr<float> > DeviceValueArrayView;
std::cout << wrapped_device_I[3];
/*
DeviceIndexArrayView row_indices (wrapped_device_I, wrapped_device_I + (N+1));
DeviceIndexArrayView column_indices(wrapped_device_J, wrapped_device_J + NNZ);
DeviceValueArrayView values (wrapped_device_V, wrapped_device_V + NNZ);
DeviceValueArrayView x (wrapped_device_x, wrapped_device_x + N);
DeviceValueArrayView b (wrapped_device_b, wrapped_device_b + N);
// std::cout << device_x[0] ;
// for(int i=0;i<NNZ;i++)std::cout << column_indices[i] << std::endl;
// combine the three array1d_views into a csr_matrix_view
typedef cusp::csr_matrix_view<DeviceIndexArrayView,
DeviceIndexArrayView,
DeviceValueArrayView> DeviceView;
// construct a csr_matrix_view from the array1d_views
DeviceView A(N, N, NNZ, row_indices, column_indices, values);
// set stopping criteria: // iteration_limit = 100 // relative_tolerance = 1e-5
cusp::verbose_monitor<float> monitor(b, 100, 1e-5);
// solve the linear system A * x = b with the Conjugate Gradient method
// cusp::krylov::bicgstab(A, x, b);*/
}
If this is not feasible, I can move over to another approach,but as I am not sure about the correctness, I am unable to decide. Any help is appreciated.

Issue with FOR loops and IF statement

Trying to display # of each combo ordered and total price. Not sure why it won't store values in A, B, and C. Novice programmer here, so be easy. Been having the problem with if statements for a while, so obviously I'm doing the whole if statement thing incorrectly.
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
int A = 0;
int B = 0;
int C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter = counter + 1)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo = A)
{
A = A + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = B)
{
B = B + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = C)
{
C = C + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
}
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
For-loop should be for (int counter = 0; counter < group; counter++)
If statement should use == for equality. = is for assignment only.
Your if statement needs to quote the character you are comparing: if (combo == 'A') {. It is also possible that you'll need to access combo as an array of characters, like so: if (combo[0] == 'A') {
I think you may need to tweak just a few things; a few compilers don't like it when you compute a double using int and there is no reason to not use double here since the program is small. Also a few syntax errors (i.e. with = instead of ==). Have you tried displaying your output in an isolated way? Something like:
main(){
double A = 1;
double B = 2;
double C = 3;
double total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
Your corrected code:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
double A = 0;
double B = 0;
double C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo == 'A')
{
A++;
}
else if (combo == 'B')
{
B++;
}
else if (combo == 'C')
{
C++;
}
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
I would also display your value for group to make sure that the if loop is even running at least once. There are a few fault points here that I would test individually.
EDIT:
Maybe test this:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Test success";
}
to see if you are even entering into your for loop.

Resources