Click here to Skip to main content
15,923,376 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: STL "for_each" algorithm. Pin
WREY5-Jan-04 1:47
WREY5-Jan-04 1:47 
GeneralRe: STL "for_each" algorithm. Pin
Jörgen Sigvardsson5-Jan-04 2:01
Jörgen Sigvardsson5-Jan-04 2:01 
GeneralRe: STL "for_each" algorithm. Pin
WREY5-Jan-04 2:23
WREY5-Jan-04 2:23 
GeneralRe: STL "for_each" algorithm. Pin
Jörgen Sigvardsson5-Jan-04 2:32
Jörgen Sigvardsson5-Jan-04 2:32 
GeneralRe: STL "for_each" algorithm. Pin
WREY5-Jan-04 2:40
WREY5-Jan-04 2:40 
GeneralRe: STL "for_each" algorithm. Pin
Sebastián Benítez6-Jan-04 6:19
Sebastián Benítez6-Jan-04 6:19 
GeneralRe: STL "for_each" algorithm. Pin
Jörgen Sigvardsson6-Jan-04 6:27
Jörgen Sigvardsson6-Jan-04 6:27 
GeneralRe: STL "for_each" algorithm. Pin
jbarton5-Jan-04 4:10
jbarton5-Jan-04 4:10 
The code example provided by Jörgen Sigvardsson was correctly updating a map of ints. It is easy to change this example to work with strings.

<br />
#include <map><br />
#include <iostream><br />
#include <utility><br />
#include <algorithm><br />
#include <string><br />
<br />
<br />
typedef std::map<std::string,std::string> StringMap;<br />
<br />
struct update<br />
   {<br />
   void operator() ( StringMap::value_type& v ) const<br />
      {<br />
      if ( v.first == "second" )<br />
         v.second = "replaced value";<br />
      }<br />
   };<br />
<br />
struct print<br />
   {<br />
   void operator() ( StringMap::value_type& v ) const<br />
      {<br />
      std::cout << "first = " << v.first << ", second = " << v.second << std::endl;<br />
      }<br />
   };<br />
<br />
int main( int argc, char* argv[] )<br />
   {<br />
   StringMap testMap;<br />
   <br />
   testMap.insert( StringMap::value_type("first", "1") );<br />
   testMap.insert( StringMap::value_type("second", "2") );<br />
   testMap.insert( StringMap::value_type("third", "3") );<br />
   testMap.insert( StringMap::value_type("fourth", "4") );<br />
   testMap.insert( StringMap::value_type("fifth", "5") );<br />
   <br />
   std::for_each(testMap.begin(), testMap.end(), print());<br />
   std::for_each(testMap.begin(), testMap.end(), update());<br />
   std::for_each(testMap.begin(), testMap.end(), print());<br />
   <br />
   return 0;<br />
   }<br />


However, as you know (from your comments in your original post), the code in the "update" functor is basically doing a find. However, since the for_each does a loop through all elements of the map, this is much less efficient than doing an actual find. If you need to do single updates to elements in a map which have particular key values, it would normally be better to just do the find and then update the element. The for_each algorithm is really only appropriate if you need to access every element in the container.

Best regards,
John
GeneralRe: STL &quot;for_each&quot; algorithm. Pin
WREY5-Jan-04 11:33
WREY5-Jan-04 11:33 
GeneralRe: STL &quot;for_each&quot; algorithm. Pin
Jörgen Sigvardsson5-Jan-04 12:36
Jörgen Sigvardsson5-Jan-04 12:36 
GeneralRe: STL &quot;for_each&quot; algorithm. Pin
jbarton6-Jan-04 5:05
jbarton6-Jan-04 5:05 
Generalstl vector Pin
Omar Alvi3-Jan-04 8:04
Omar Alvi3-Jan-04 8:04 
GeneralRe: stl vector Pin
valikac3-Jan-04 17:14
valikac3-Jan-04 17:14 
GeneralRe: stl vector Pin
Lim Bio Liong9-Jan-04 14:52
Lim Bio Liong9-Jan-04 14:52 
Generalnew for ATL Pin
utkarsharma1-Jan-04 22:15
utkarsharma1-Jan-04 22:15 
GeneralRe: new for ATL Pin
Jörgen Sigvardsson5-Jan-04 12:40
Jörgen Sigvardsson5-Jan-04 12:40 
GeneralRe: new for ATL Pin
utkarsharma6-Jan-04 3:15
utkarsharma6-Jan-04 3:15 
GeneralATL ActiveX Stock Property Pin
Hesham Amin1-Jan-04 2:59
Hesham Amin1-Jan-04 2:59 
GeneralRe: ATL ActiveX Stock Property Pin
Jörgen Sigvardsson1-Jan-04 4:54
Jörgen Sigvardsson1-Jan-04 4:54 
GeneralRe: ATL ActiveX Stock Property Pin
Hesham Amin1-Jan-04 11:59
Hesham Amin1-Jan-04 11:59 
GeneralRe: ATL ActiveX Stock Property Pin
Jörgen Sigvardsson1-Jan-04 12:38
Jörgen Sigvardsson1-Jan-04 12:38 
GeneralRe: ATL ActiveX Stock Property Pin
Hesham Amin1-Jan-04 12:49
Hesham Amin1-Jan-04 12:49 
GeneralC++ compile error C3358: CSoapHandler symbol not found Pin
nativespirits30-Dec-03 10:17
nativespirits30-Dec-03 10:17 
GeneralRe: C++ compile error C3358: CSoapHandler symbol not found Pin
nativespirits30-Dec-03 12:03
nativespirits30-Dec-03 12:03 
GeneralMFC class with class wizard in ATL Pin
cercis30-Dec-03 8:16
cercis30-Dec-03 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.