• Advertisement
Sign in to follow this  

(c++) Iterating a std::set, how to get the variables from the set?

This topic is 4295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm trying to iterate through the variables of a std::set<string> I've got:
set<string> words1, words2;

for(set<string>::iterator i=words1.begin(); i!=words1.end(); i++) {
   set<string>::iterator n = find(entry->words2.begin(), entry->words2.end(), i); //the i at the end throws an error
//btw the i is for the search variable, putting in a string, eg
// "hello" works fine
}                 

The error it throws is:
Quote:
In function `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::string>, _Tp = std::_Rb_tree_const_iterator<std::string>]':
I thought all I had to do was pass the 'i' to whatever to be able to use the iterated variable, what am I doing wrong? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
In C++, an iterator is like a pointer in many respects. The iterator name by itself represents a position somewhere, and to get the value at that position, you must use * to 'dereference' the iterator. Thus, you'd use "*i" to get the value at the position "i" references.

Share this post


Link to post
Share on other sites
Quote:
Original post by izhbq412
std::find performs linear search. Sets are sorted collections and you can search much faster by using a binary search (std::set's find method).


So if I add to a set say:

"a"
"g"
"c"


And I iterate through the set, it will return:

"a"
"c"
"g"

?

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrarius
In C++, an iterator is like a pointer in many respects. The iterator name by itself represents a position somewhere, and to get the value at that position, you must use * to 'dereference' the iterator. Thus, you'd use "*i" to get the value at the position "i" references.


Quoted for being the answer. As an aside:

Quote:
The error it throws is:
Quote:
In function `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::string>, _Tp = std::_Rb_tree_const_iterator<std::string>]':


That's not the error. That's the notification of where it is. A quick decoding of all the relevant parts:

In function `_InputIterator std::find (_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::string>, _Tp = std::_Rb_tree_const_iterator<std::string>]':

Translation: In std::find

In function `_InputIterator std::find (_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::string>, _Tp = std::_Rb_tree_const_iterator<std::string>]':

Translation: Which returns an iterator, accepts two iterators and something else (we can guess: a constant value by reference)

In function `_InputIterator std::find (_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::string>, _Tp = std::_Rb_tree_const_iterator<std::string>]':

Translation: Which we gave a std::map< std::string >::const_iterator for the iterator and an iterator for the value...

The next error probably was something along the lines of "cannot convert from _____ to ____" in more words.

You may want to look into STLFilt, a tool designed to help clear up the meaning of these errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Quote:
Original post by izhbq412
std::find performs linear search. Sets are sorted collections and you can search much faster by using a binary search (std::set's find method).


So if I add to a set say:

"a"
"g"
"c"


And I iterate through the set, it will return:

"a"
"c"
"g"

?
Yes.

Share this post


Link to post
Share on other sites
Quote:

Translation: Which we gave a std::map< std::string >::const_iterator for the iterator


Surely you meant std::set< std::string >::const_iterator, for numerous rather technical reasons?

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Quote:

Translation: Which we gave a std::map< std::string >::const_iterator for the iterator


Surely you meant std::set< std::string >::const_iterator, for numerous rather technical reasons?


Pay no attention to the man behind the curtain.

(yes)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement