code improvement question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    code improvement question

    Consider the following piece of code.

    vector<WordAndL ineNumbers>::it erator iter = find(unique_wor ds.begin(),
    unique_words.en d(), str);

    if (iter != unique_words.en d())
    iter->insert(cur_lin e_number);

    Can these three lines be combined ?

    Kindly reply

    Thanks
    V.Subramanian

  • Barry

    #2
    Re: code improvement question

    subramanian100i n@yahoo.com, India wrote:
    Consider the following piece of code.
    >
    vector<WordAndL ineNumbers>::it erator iter = find(unique_wor ds.begin(),
    unique_words.en d(), str);
    >
    if (iter != unique_words.en d())
    iter->insert(cur_lin e_number);
    >
    Can these three lines be combined ?
    >
    Hackery code like this:

    for
    (vector<WordAnd LineNumbers>::i terator iter
    = find(unique_wor ds.begin();
    iter != unique_words.en d();
    iter = unique_words.en d()
    )
    {
    iter->insert(cur_lin e_number);
    }

    I don't encourage this

    See from your code, why not use set instead of vector to ensure the
    uniqueness of the words

    Kindly reply
    >

    Comment

    • rolkA

      #3
      Re: code improvement question

      On 30 sep, 09:17, "subramanian10. ..@yahoo.com, India"
      <subramanian10. ..@yahoo.comwro te:
      Consider the following piece of code.
      >
      vector<WordAndL ineNumbers>::it erator iter = find(unique_wor ds.begin(),
      unique_words.en d(), str);
      >
      if (iter != unique_words.en d())
      iter->insert(cur_lin e_number);
      >
      Can these three lines be combined ?
      >
      Kindly reply
      >
      Thanks
      V.Subramanian
      Hi,
      You could use an insert iterator, but it will insert à the end if no
      match is found :

      inserter(unique _words, std::find(uniqu e_words.begin() ,
      unique_words.en d(), str)) = cur_line_number ; // /!\ doesn't do what
      you want

      I wonder how we coud achieve this, since there isn't any "insert_if"
      utility in C++
      But you'd rather focus on clarity: your code is comprehensible and
      there isn't any performance problem with it, so why modify it ?

      Comment

      • Daniel T.

        #4
        Re: code improvement question

        <subramanian100 in@yahoo.comwro te:
        Consider the following piece of code.
        >
        vector<WordAndL ineNumbers>::it erator iter = find(unique_wor ds.begin(),
        unique_words.en d(), str);
        >
        if (iter != unique_words.en d())
        iter->insert(cur_lin e_number);
        >
        Can these three lines be combined ?
        Yes, but only if you make a "null object" and put it at the end of your
        unique_words vector. The null object is designed to always end up at the
        end of the unique words list when sorted, and it is set up to ignore the
        insert. Something like this for example:

        class WordAndLineNumb ers
        {
        string _word;
        int lineNumber;
        public:
        WordAndLineNumb ers(): _word(), lineNumber() { }
        WordAndLineNumb ers( string s ): _word( s ), lineNumber() { }
        void insert( int n ) {
        if ( _word != "" )
        lineNumber = n;
        }
        string word() const { return _word; }
        };

        bool operator<( const WordAndLineNumb ers& lhs,
        const WordAndLineNumb ers& rhs )
        {
        if ( lhs.word() == "" ) return false;
        else if ( rhs.word() == "" ) return true;
        else
        return lhs.word() < rhs.word();
        }

        bool operator==( const WordAndLineNumb ers& lhs,
        const WordAndLineNumb ers& rhs )
        {
        return lhs.word() == rhs.word();
        }

        bool operator==( const WordAndLineNumb ers& w, const string& s )
        {
        if ( w.word() == "" ) return true;
        return w.word() == s;
        }

        int main()
        {
        vector<WordAndL ineNumbersuniqu e_words;
        unique_words.pu sh_back( WordAndLineNumb ers() );
        unique_words.pu sh_back( WordAndLineNumb ers( "goodby" ) );
        string str = "hello";
        int cur_line_number = 3;
        sort( unique_words.be gin(), unique_words.en d() );
        unique( unique_words.be gin(), unique_words.en d() );

        assert( find( unique_words.be gin(), unique_words.en d(), str ) !=
        unique_words.en d() );
        find( unique_words.be gin(), unique_words.en d(), str )->
        insert( cur_line_number );
        }

        Comment

        Working...