Thread: Maps with ints[]

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Maps with ints[]

    Hey every1,

    I'm trying to have a Map with a string as key, and a 2 (or more) element array as data. Something like
    Code:
     map <string, int[2]> myMap
    I'm getting errors when i try to load data into the Map.

    readin.cpp(23) : error C2106: '=' : left operand must be l-value

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <map>
    
    using std::map;
    using namespace std;
    
    int main(){
    	
    	map <string, int[2]> b;
    	map <string, int[2]>::iterator iter;
    
    	string rob = "ROB";
    
    	int c[2];
    
    	c[0] = 1;
    	c[1] = 0;
    
    	b[rob] = c;
    
    	iter = b.begin();
             cout << iter->second << endl;    
    }
    i've tried alternatives to the line
    Code:
    b[rob] = c;
    like
    Code:
    b[rob] = c[];
    which gave me: error C2059: syntax error : ']' or
    Code:
    b[rob] = c[8];
    which gave me: error C2440: '=' : cannot convert from 'int' to 'int [2]'

    anyone have any ideas how i should load the data?

    Cheers,

    Rob.

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    You can't use the assignment operator with arrays except initialization, why not use a vector instead of an array?
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Or since it is always 2 ints, use a pair (std::pair<int, int>).
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main(){
    	
        map <string, std::pair<int,int> > b;
        map <string, std::pair<int,int> >::iterator iter;
    
        string rob = "ROB";
    
        b[rob] = std::make_pair(1, 0);
    
        iter = b.begin();
        cout << iter->second.first << iter->second.second << endl;    
    }
    By the way, is that cout statement just an example? It would just print the pointer address of the first element of the array. If you do want to print out the ints, you'll have to do a little more work.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    vectors seem to do the trick, nice1, cheers every1.

    Only thing is i'm gettin loads (like 100) warnings with vectors

    Code:
    c:\program files\microsoft visual studio\vc98\include\xtree(120) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
    std::vector<int,std::allocator<int> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<int,std::allocator<int> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocat
    or<std::vector<int,std::allocator<int> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<int,std::allocator<int> > > >' : identifier was truncated to '255' characters in the debug
     information
            c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
    char>,std::allocator<char> > const ,std::vector<int,std::allocator<int> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<int,std::allocator<int> >,std::less<std::basic_string<char,std::char_traits<char>,s
    td::allocator<char> > >,std::allocator<std::vector<int,std::allocator<int> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<int,std::allocator<int> > > >' being compiled
            c:\neo_soft\test\readin.cpp(38) : see reference to class template instantiation 'std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<int,std::allocator<int> >,std::less<std::basic_string<char,std::char_t
    raits<char>,std::allocator<char> > >,std::allocator<std::vector<int,std::allocator<int> > > >' being compiled
    it's the "identifier was truncated to 'number' characters in the debug information" error but it's for all these files in the MS include directory, map and xtree and all this. I've disabled warnings for my program but these other files that i've never come across (xtree) are throwin up loads of errors,

    Any1 got any idea's?

    Cheers,

    Rob.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    This is error is common when using standard containers. You can get rid of it using:

    #pragma warning(disable: 4786)

    Sometimes it works if you put it at the top of your code, but sometimes not. You can open the file with the error and put that line under the includes. Normally you don't want to edit those files, but this is usually fine. Put it on each file that has that warning (like xtree in this case).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading maps
    By divineleft in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2006, 10:09 AM
  2. Hash_map, STL maps query
    By alvifarooq in forum C++ Programming
    Replies: 15
    Last Post: 06-07-2005, 09:07 AM
  3. DDX/DDV and Message Maps
    By axr0284 in forum Windows Programming
    Replies: 2
    Last Post: 01-07-2005, 08:55 AM
  4. maps as reference?
    By robquigley in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 09:12 AM
  5. Finding terrain maps...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-25-2001, 07:44 PM