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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Jørgensen

    #1

    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_t ypes.begin();
    it != possible_data_t ypes.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::__no rmal_iterator<c onst 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
  • Daniel T.

    #2
    Re: iterating through vector&lt;strin g&gt;'s.... error

    Martin Jørgensen <hotmail_spam@h otmail.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_t ypes.begin();
    it != possible_data_t ypes.end(); ++it)
    {
    data_type++;
    cout << data_type << ": " << *it << endl;
    >
    if( *it.find("RhoCp ") != string::npos )
    Use either:

    (*it).find("Rho Cp")

    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.

    Comment

    • Clark S. Cox III

      #3
      Re: iterating through vector&lt;strin g&gt;'s.... error

      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_t ypes.begin();
      it != possible_data_t ypes.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
      clarkcox3@gmail .com

      Comment

      • Daniel T.

        #4
        Re: iterating through vector&lt;strin g&gt;'s.... error

        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<strin g>& possible_data_t ypes )
        {
        string color_line = *find_if( possible_data_t ypes.begin(),
        possible_data_t ypes.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;
        }
        };

        Comment

        • Daniel T.

          #5
          Re: iterating through vector&lt;strin g&gt;'s.... error

          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<strin g>& possible_data_t ypes)
          {
          string color_line = *find_if(possib le_data_types.b egin(),
          possible_data_t ypes.end(), Contains("RhoCp "));
          }

          The above uses:

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

          --
          To send me email, put "sheltie" in the subject.

          Comment

          • Martin Jørgensen

            #6
            Re: iterating through vector&lt;strin g&gt;'s.... error

            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

            Comment

            Working...