STL iterator ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sd2004

    #1

    STL iterator ?

    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::npo s){
    v.push_back(s.s ubstr(i,j-i));
    i = ++j;
    j=s.find(c,j);
    if (j==string::npo s)
    v.push_back(s.s ubstr(i,s.lengt h()));
    }

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

    }
    int main (){

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

  • Mateusz Łoskot

    #2
    Re: STL iterator ?

    sd2004 wrote:[color=blue]
    > vector<string>: :iterator pos;
    > pos = find (v.begin(),v.en d(),2);[/color]
    ^^^^^

    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
    --
    Mateusz Łoskot

    Comment

    • Luke Meyers

      #3
      Re: STL iterator ?

      sd2004 wrote:[color=blue]
      > Could someone please tell me why the code below not compile ?
      > Thanks in advance for your help.[/color]

      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

      Comment

      • sd2004

        #4
        Re: STL iterator ?

        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::npo s){
        v.push_back(s.s ubstr(i,j-i));
        i = ++j;
        j=s.find(c,j);
        if (j==string::npo s)
        v.push_back(s.s ubstr(i,s.lengt h()));
        }

        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;3 33.2");
        // split (s,delim);
        results= split (s,delim,loc);
        cout << "RESULTS IS: "<<results<<end l;
        }

        Comment

        • Mateusz Łoskot

          #5
          Re: STL iterator ?

          sd2004 wrote:[color=blue]
          > I changed the code a little bit.
          > I now compile , however , the program is running into an endless loop.
          > Could someone please help ?[/color]

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

          "Accelerate d C++" , A. Koenig and B. Moo

          Review:


          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
          --
          Mateusz Łoskot

          Comment

          Working...