STL iterator ?

S

sd2004

Could someone please tell me why the code below not compile ?
Thanks in advance for your help.

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

void split (const string& s, char c){
vector<string> v;
string:: size_type i=0;
float data;
string:: size_type j=s.find(c);

while (j!=string::npos){
v.push_back(s.substr(i,j-i));
i = ++j;
j=s.find(c,j);
if (j==string::npos)
v.push_back(s.substr(i,s.length()));
}

vector<string>::iterator pos;
pos = find (v.begin(),v.end(),2);
cout << "pos: " <<*pos <<endl;

}
int main (){

double results;
char const delim = ';';
string s("id;2;address;4;5;6;7");
split (s,delim);
}
 
G

Guest

sd2004 said:
vector<string>::iterator pos;
pos = find (v.begin(),v.end(),2);
^^^^^

Are you expect find will do implicit conversion from int (number 2)
to string?
Read about std::find algorithm:
third parameter is the value to be searched for.
So, if you are searching through vector of strings then you
have to search against string.

In example:
pos = find (v.begin(), v.end(), "2");

Cheers
 
L

Luke Meyers

sd2004 said:
Could someone please tell me why the code below not compile ?
Thanks in advance for your help.

Please post the text of error messages, and indicate the line on which
they occur. Saves the rest of us the trouble, so we can help you more
efficiently. We like efficiency! ;)

Luke
 
S

sd2004

I changed the code a little bit.
I now compile , however , the program is running into an endless loop.
Could someone please help ?
Perhap, show me a better way of coding .
Thank in advance for your time.

#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
using namespace std;

//void split (const string& s, char c,const int here){
float split (const string& s, char c, const int here){
vector<string> v;
string:: size_type i=0;
float data;
string:: size_type j=s.find(c);

while (j!=string::npos){
v.push_back(s.substr(i,j-i));
i = ++j;
j=s.find(c,j);
if (j==string::npos)
v.push_back(s.substr(i,s.length()));
}

for (vector<string>::size_type f=0;f!=v.size();++f)
if (f=here){
cout << "here: "<< here<<endl;
data = atof(v[f].c_str());

}
return data;

}
int main (){

double results;
char const delim = ';';
int loc =3;
string s("Account;124.5;1.0;1323.31;333.2");
// split (s,delim);
results= split (s,delim,loc);
cout << "RESULTS IS: "<<results<<endl;
}
 
G

Guest

sd2004 said:
I changed the code a little bit.
I now compile , however , the program is running into an endless loop.
Could someone please help ?

Please, let me to recommend you a book which I hope will be very helpful.

"Accelerated C++" , A. Koenig and B. Moo

Review:
www.accu.org/bookreviews/public/reviews/a/a002212.htm

Look at chapter 5.6 and you will find implementation of string split
function with very clear explanation of all "whats" and "whys" related
to the solution.
Cheers
 

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top