473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
5 2515
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
2
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
5
by: Peithon | last post by:
Hi, I'm trying to create a vector of strings and print the contents using an iterator but I'm getting an error with my very first string. Can anyone help?
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.