Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL find

Status
Not open for further replies.

hectorDUQUE

Programmer
Jan 29, 2007
23
hi guys,
i have sintax problems using a STL find function :-(

Code:
   typedef std::set<string> groupOfFileNames_type;
   typedef std::map<string, groupOfFileNames_type> map_groupFN_type;


   typedef std::vector<string> resultsGroups_type;
   typedef std::map<string, resultsGroups_type> map_completeGroups2resultsGroupsByOrders_type;

   map_groupFN_type completeGroup2groups;
   map_group_type   group2numbers;

    for (map_groupFN_type::const_iterator cur_node = completeGroup2groups.begin(),
        end_node = completeGroup2groups.end(); cur_node != end_node; ++cur_node)
    {

        for (groupOfFileNames_type::const_iterator cur_edge = cur_node->second.begin(),
            end_edge = cur_node->second.end(); cur_edge != end_edge; ++cur_edge)
        {

	
          map_group_type::const_iterator group2numbersIterator;

	  map_group_type::const_iterator group2numbersNode0x = group2numbers.begin();
	  map_group_type::const_iterator group2numbersNodeNx = group2numbers.end();

	  group2numbersIterator = find (group2numbersNode0x, group2numbersNodeNx,  "something");
 
        }//for
    }//for



i am trying to match info between two maps.

the find function (STL searching algorithm) has this message in compilation time:

Code:
adResultGroupsFIND.Tpo -c hD_loadResultGroupsFIND.cxx  -fPIC -DPIC -o .libs/hD_loadResultGroupsFIND.o
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_algo.h: In function '_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> > > >, _Tp = char [9]]':
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_algo.h:316:   instantiated from '_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> > > >, _Tp = char [9]]'
hD_loadResultGroupsFIND.cxx:57:   instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_algo.h:173: error: no match for 'operator==' in '__first. std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> > >]() == __val'
make[2]: *** [hD_loadResultGroupsFIND.lo] Error 1
make[2]: Leaving directory `/home/duque32/hD_projects/hD_rouletteGambler/map'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/duque32/hD_projects/hD_rouletteGambler'
make: *** [all] Error 2


i wonder if i can use the find STL seaching algortihm with maps?

thanks in advance for help.

hector
 
Are you trying to find a map key with "something" or map data with "something"?
What does the map_group_type typedef look like?
If you're searching through a map, I think you need to use the find() member of the map class instead of the one in <algorithm>.
 
Are you trying to find a map key with "something" or map data with "something"?"
R/ a map key with "something"


"What does the map_group_type typedef look like?"

Code:
   typedef std::set<int>    groupOfNumbers_type;
   typedef std::map<string, groupOfNumbers_type> map_group_type;

"If you're searching through a map, I think you need to use the find() member of the map class instead of the one in <algorithm>."

R/ the STL manual (stlfreedoc) says "those algorithms are mostly used on STL containers"
I hope it includes this algortihm for maps :-(

my concern is that i would like to apply a lot of those algorithms to the key or data parts of maps. If not, i have to move info from maps to vectors, before aplying them; isn't it ?

regards,

hector

 
So why not do this instead:
Code:
group2numbersIterator = group2numbers.find( "something" );
It's always better to use the find() & sort() functions that come with STL containers rather than the generic version of those algorithms since the generic functions can't be optimized for use with that type of container.
 
thanks cpjust, it works.

what about the map data part?
wit this code:

Code:
groupOfNumbers_type::const_iterator groupOfNumbersIterator;
groupOfNumbersIterator = group2numbers->second.find( "28" );

i got:

Code:
hD_loadResultGroupsFIND.cxx:69: error: base operand of '->' has non-pointer type 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> > > > >'


hector

 
The important part of that error is:
Code:
base operand of '->' has non-pointer type 'std::map
The rest is just STL type expansion which is too hard to read and irrelevant.
It's telling you that since group2numbers is a regular std::map, not a std::map*, so you can't use -> on it.
Try this instead:
Code:
groupOfNumbersIterator = group2numbers.second.find( "28" );
 
i've done before:

Code:
hD_loadResultGroupsFIND.cxx:70: error: 'class std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<int, std::less<int>, std::allocator<int> > > > >' has no member named 'second'

has no member named 'second' :-(
 
Oops, what was I thinking...
the second field is used with a map iterator, not the map itself. You'll need to replace group2numbers with an iterator to an element of that map. Then change the . back to a ->
 
ohh, this stuff is getting me crazy ...

this code works for the map data part:

Code:
	    groupOfNumbers_type::const_iterator groupOfNumbersIterator = group2numbersIterator->second.begin();
	    groupOfNumbersIterator = group2numbersIterator->second.find( 28 );

thanks cpjust

hector

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top