Sign in to follow this  
johnnyBravo

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

Recommended Posts

johnnyBravo    100
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
Extrarius    1412
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
johnnyBravo    100
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
MaulingMonkey    1730
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
izhbq412    204
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
Deyja    920
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this