iterating through vector<string>'s.... error

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?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
 
D

Daniel T.

Martin Jørgensen said:
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 )

Use either:

(*it).find("RhoCp")

or (preferred)

it->find("RhoCp")

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???

No the code you wrote tries to call find on the iterator and then
dereference the result.
 
C

Clark S. Cox III

Martin said:
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;
[snip]

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:

"*it.find(...)" is functionally equivalent to "*(it.find(...))", which
is obviously wrong (because, as you noted, the iterator does not have a
find() member function, you want:

"(*it).find(...)" or "it->find(...)" instead.
 
D

Daniel T.

Martin said:
The piece of code I'm struggling with is so simple, that I hope nobody
wants a complete example for answering the question:

Here is a simpler method of doing what you want...

If you know that at least one of the members will have the approprate
string in it then:

void fn(vector<string>& possible_data_types )
{
string color_line = *find_if( possible_data_types.begin(),
possible_data_types.end(),
Contains( "RhoCp" ) );
}

The above uses:

struct Contains : unary_function< string, bool >
{
string val;
Contains( const string& v ): val( v ) { }
bool operator()( const string& data ) const {
return data.find( val ) != string::npos;
}
};
 
D

Daniel T.

Martin said:
The piece of code I'm struggling with is so simple, that I hope nobody
wants a complete example for answering the question:

Here is a simpler method of doing what you want...

If you know that at least one of the members will have the approprate
string in it then:

void fn(vector<string>& possible_data_types)
{
string color_line = *find_if(possible_data_types.begin(),
possible_data_types.end(), Contains("RhoCp"));
}

The above uses:

struct Contains : unary_function< string, bool >
{
string val;
Contains(const string& v): val(v) { }
bool operator()(const string& data) const {
return data.find(val) != string::npos;
}
};
 
G

Guest

Clark said:
Martin Jørgensen wrote: -snip-

"*it.find(...)" is functionally equivalent to "*(it.find(...))", which
is obviously wrong (because, as you noted, the iterator does not have a
find() member function, you want:

"(*it).find(...)" or "it->find(...)" instead.

Great, thanks a lot. Also to Daniel...


Best regards
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top