code improvement question

S

subramanian100in

Consider the following piece of code.

vector<WordAndLineNumbers>::iterator iter = find(unique_words.begin(),
unique_words.end(), str);

if (iter != unique_words.end())
iter->insert(cur_line_number);

Can these three lines be combined ?

Kindly reply

Thanks
V.Subramanian
 
B

Barry

Consider the following piece of code.

vector<WordAndLineNumbers>::iterator iter = find(unique_words.begin(),
unique_words.end(), str);

if (iter != unique_words.end())
iter->insert(cur_line_number);

Can these three lines be combined ?
Hackery code like this:

for
(vector<WordAndLineNumbers>::iterator iter
= find(unique_words.begin();
iter != unique_words.end();
iter = unique_words.end()
)
{
iter->insert(cur_line_number);
}

I don't encourage this

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

rolkA

Consider the following piece of code.

vector<WordAndLineNumbers>::iterator iter = find(unique_words.begin(),
unique_words.end(), str);

if (iter != unique_words.end())
iter->insert(cur_line_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(unique_words.begin(),
unique_words.end(), 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 ?
 
D

Daniel T.

Consider the following piece of code.

vector<WordAndLineNumbers>::iterator iter = find(unique_words.begin(),
unique_words.end(), str);

if (iter != unique_words.end())
iter->insert(cur_line_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 WordAndLineNumbers
{
string _word;
int lineNumber;
public:
WordAndLineNumbers(): _word(), lineNumber() { }
WordAndLineNumbers( string s ): _word( s ), lineNumber() { }
void insert( int n ) {
if ( _word != "" )
lineNumber = n;
}
string word() const { return _word; }
};

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

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

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

int main()
{
vector<WordAndLineNumbers> unique_words;
unique_words.push_back( WordAndLineNumbers() );
unique_words.push_back( WordAndLineNumbers( "goodby" ) );
string str = "hello";
int cur_line_number = 3;
sort( unique_words.begin(), unique_words.end() );
unique( unique_words.begin(), unique_words.end() );

assert( find( unique_words.begin(), unique_words.end(), str ) !=
unique_words.end() );
find( unique_words.begin(), unique_words.end(), str )->
insert( cur_line_number );
}
 

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
474,201
Messages
2,571,047
Members
47,646
Latest member
xayaci5906

Latest Threads

Top