?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi,
The piece of code I'm struggling with is so simple, that I hope nobody
wants a complete example for answering the question:
--------
string color_line;
int data_type = 0;
for( vector<string>::const_iterator it = possible_data_types.begin();
it != possible_data_types.end(); ++it)
{
data_type++;
cout << data_type << ": " << *it << endl;
if( *it.find("RhoCp") != string::npos )
color_line = *it;
}
--------
Gives output like this:
--------
1: SCALARS Temperature double 1
2: SCALARS Porosities double 1
3: SCALARS RhoCp double 1
4: SCALARS Cell_energy double 1
5: SCALARS Residuals double 1
--------
So: When the for-loop has just written line 3 out (with cout), I want to
copy that particular string into "color_line". The result should be the
same as using:
string color_line = "SCALARS RhoCp double 1";
But I'm programming it this way, because RhoCp doesn't always has to be
in line 3 (if it even exists). The line: " if( *it.find("RhoCp") !=
string::npos ) " doesn't work (doesn't compile). It gives:
--------
output_to_latex.cpp: In function 'int main(int, char**)':
output_to_latex.cpp:370: error: 'class
__gnu_cxx::__normal_iterator<const std::string*,
std::vector<std::string, std::allocator<std::string> > >' has no member
named 'find'
make: *** [output_to_latex] Error 1
--------
Can anyone tell why it doesn't work and how to fix the problem? TIA.
I assume it's something like: An iterator doesn't have the
"find"-function in it. But I thought I was dereferencing the iterator,
so I would be calling find from the string-object that the vector holds???
Best regards
Martin Jørgensen
The piece of code I'm struggling with is so simple, that I hope nobody
wants a complete example for answering the question:
--------
string color_line;
int data_type = 0;
for( vector<string>::const_iterator it = possible_data_types.begin();
it != possible_data_types.end(); ++it)
{
data_type++;
cout << data_type << ": " << *it << endl;
if( *it.find("RhoCp") != string::npos )
color_line = *it;
}
--------
Gives output like this:
--------
1: SCALARS Temperature double 1
2: SCALARS Porosities double 1
3: SCALARS RhoCp double 1
4: SCALARS Cell_energy double 1
5: SCALARS Residuals double 1
--------
So: When the for-loop has just written line 3 out (with cout), I want to
copy that particular string into "color_line". The result should be the
same as using:
string color_line = "SCALARS RhoCp double 1";
But I'm programming it this way, because RhoCp doesn't always has to be
in line 3 (if it even exists). The line: " if( *it.find("RhoCp") !=
string::npos ) " doesn't work (doesn't compile). It gives:
--------
output_to_latex.cpp: In function 'int main(int, char**)':
output_to_latex.cpp:370: error: 'class
__gnu_cxx::__normal_iterator<const std::string*,
std::vector<std::string, std::allocator<std::string> > >' has no member
named 'find'
make: *** [output_to_latex] Error 1
--------
Can anyone tell why it doesn't work and how to fix the problem? TIA.
I assume it's something like: An iterator doesn't have the
"find"-function in it. But I thought I was dereferencing the iterator,
so I would be calling find from the string-object that the vector holds???
Best regards
Martin Jørgensen