By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Nov 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Martin Jørgensen <ho**********@hotmail.comwrote:
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.

--
To send me email, put "sheltie" in the subject.
Nov 5 '06 #2

P: n/a
Martin Jørgensen wrote:
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.

--
Clark S. Cox III
cl*******@gmail.com
Nov 5 '06 #3

P: n/a
Martin Jørgensen wrote:
>
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;
}
};

Nov 5 '06 #4

P: n/a
Martin Jørgensen wrote:
>
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;
}
};

--
To send me email, put "sheltie" in the subject.
Nov 5 '06 #5

P: n/a
Clark S. Cox III wrote:
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Nov 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.