Thread: STL Map - simple question on looping

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    STL Map - simple question on looping

    I have the following:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <map>
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
        map <int, char> mp;
        map<int, char> :: iterator it;
    
    
        mp.insert(map <int, char> :: value_type(1,'A'));           //storing in sorted order -> A,B,C,D,E
        mp.insert(map <int, char> :: value_type(3,'C'));
        mp.insert(map <int, char> :: value_type(2,'B'));
        mp.insert(map <int, char> :: value_type(5,'E'));
        mp.insert(map <int, char> :: value_type(4,'D'));
    
    
    
    
        it = mp.begin();
    
    
       while(it != mp.end()){
                 cout << (*it).first <<" ";                        // prints key
                 cout << (*it).second << endl;                       //print data
                 it++;
                 }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Is there a way to implement a loop to input the data sets? I have tried using the iterator as a loop counter but i end up with various errors. i guess that is not allowed.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    One possibility
    Code:
    map <int, char> mp;
    for( int i = 0; i < 5; ++i)
       mp[i+1]='A' + i;
    Kurt

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Quote Originally Posted by ZuK View Post
    One possibility
    Code:
    map <int, char> mp;
    for( int i = 0; i < 5; ++i)
       mp[i+1]='A' + i;
    Kurt
    following error:

    Code:
    ||In function 'int main(int, char**)':|
    12|error: no match for 'operator=' in 'mp[(i + 1)] = 66'|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|251|note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int, _Tp = char, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, char> >]|
    ||=== Build finished: 1 errors, 0 warnings ===|

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <iostream>
    #include <map>
    using namespace std;
    int main() {
        map <int, char> mp;
        for( int i = 0; i < 5; ++i)
            mp[i+1]='A' + i;
        for ( map<int, char>::iterator it = mp.begin(); it != mp.end(); ++it ){
            cout << (*it).first <<" ";                        // prints key
            cout << (*it).second << endl;                      //print data
        }
    }
    works fine for me
    my output:
    1 A
    2 B
    3 C
    4 D
    5 E

    Kurt
    Last edited by ZuK; 05-04-2012 at 12:17 PM. Reason: Indentation

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by skg29 View Post
    following error:

    Code:
    ||In function 'int main(int, char**)':|
    12|error: no match for 'operator=' in 'mp[(i + 1)] = 66'|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|251|note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int, _Tp = char, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, char> >]|
    ||=== Build finished: 1 errors, 0 warnings ===|
    It seems to me like you must have declared an array of maps to get such an error, because it thinks you're trying to invoke the copy-constructor of the map itself. That would not happen if you just used the [] operator on a single map. Also, why are you trying to stick ints like 66 in there? It takes characters and if you want whatever character 66 is then just insert that character as a character.

    Post the actual code you tried.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    ok it worked. But how about making it more general. for example i want to build a map with <char><char> where the user inputs the data and decides when to end the loop.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What did you try so far ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping question
    By ranjit89 in forum C Programming
    Replies: 5
    Last Post: 11-01-2011, 05:35 AM
  2. Help Me,, I have 9 Looping Question for C / C++
    By xcurialz in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2010, 01:53 AM
  3. Looping Question
    By ewandougie in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2009, 08:21 PM
  4. Looping question
    By sparkyf in forum C Programming
    Replies: 2
    Last Post: 10-25-2003, 03:21 PM
  5. C++ looping question
    By deepthought in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2001, 01:33 PM