C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 4th, 2006, 10:33 AM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
Need Help with STL (map)

Hi,

I'm trying to creat a <map> from the STL that will be somthing like that :

map<string,<vector>> map_name;

I defined it like that :

Code:
typedef map <string, vector<int>,ltstr> word_ret;

class	wordinfo
{
	private:
		word_ret	mapi;

	public:
		void	add_info(const string);
		
		wordinfo();
};


but i'm not sure how to insert data into the map

i want that each of the map cells will have his own vector

the "ltstr" is small struct that defines which string is bigger

thanks ahead =)

Reply With Quote
  #2  
Old March 4th, 2006, 04:13 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 29 m 29 sec
Reputation Power: 4
Quick example, with char * instead of string though:

Code:
#include <map>
#include <vector>
#include <iostream>

using namespace std;

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main(void)
{
  vector<int> *v;
  map<const char *, vector<int>, ltstr > complicatedMap;

  v = new vector<int>();

  // like this:
  complicatedMap["blah"] = *v;
}


Reply With Quote
  #3  
Old March 4th, 2006, 10:51 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
note that with Icon's code you'll have to explicitly delete all of those vectors.
you can just do this instead, and the vectors will get automatically destructed when the map goes out of scope:
Code:
#include <map>
#include <vector>
using namespace std;

struct ltstr
{
  bool operator()(const char *s1, const char *s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main()
{
  map<const char*, vector<int>, ltstr> complicatedMap;

  // like this:
  complicatedMap["blah"] = vector<int>();

  return 0;
}

Reply With Quote
  #4  
Old March 5th, 2006, 04:42 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 29 m 29 sec
Reputation Power: 4
Agreed. It depends on what he wants to do with it and where the vector comes from right? When writing c++ i always find myself thinking so much about these questions, the memory issues, that it really bites into my time of thinking of the problem (i.e. algorithm) at hand.. Many people write c-style programs with c++ and do not use the mechanism you describe ubergeek, which I prefer by the way.

Reply With Quote
  #5  
Old March 5th, 2006, 06:29 PM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
it seems more logicaly use it with the vector commands

like :

Code:
map_name[str_var].push_back(value);


- so i did it

i pushed some data in - and then tried to read it back to and see if all fine ...

well new problems

i tierd to pull the data like that :

Code:
for(iter = mapi.begin(); iter != mapi.end(); iter++)
    {
        cout << iter->second.pop_back;
        cout << (*iter).first << endl;   
    }


well it didn't work - the first line (iter->second.pop_back) gave me that error :

Quote:
fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++


the second line worked - so i figured how to see the first of the pair - what should i do to pull the second ?

thanks ahead

Reply With Quote
  #6  
Old March 5th, 2006, 07:36 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
you have to actually call pop_back by including parentheses (even though you aren't passing it any parameters, you need to do that, or the compiler thinks you want a function pointer (which should do something, not what you want but something, but apparently the compiler chokes on it).
Code:
cout << iter->second.pop_back();

Reply With Quote
  #7  
Old March 5th, 2006, 07:50 PM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
oh it's just typo - i've put there () ...

but i made another mistake - it seems that pop_back don't return the value - therefor how should i show the content of the vector ? with ".front" ?

Reply With Quote
  #8  
Old March 5th, 2006, 07:52 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
odd...did you try using (*iter).second like you did in the second line (instead of iter->second)? you could also try putting parentheses in:
Code:
cout << (iter->second).pop_back();
//or
cout << ((*iter).second).pop_back();

Reply With Quote
  #9  
Old March 5th, 2006, 08:00 PM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
i'm kinda lost with those iterators

I believe that i made mistake and stucked the iterator and i'm always pointing to the same cell in the vector

pfff those STL should be user friendly

Reply With Quote
  #10  
Old March 5th, 2006, 08:01 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
can I see all of your code, including the part that loads values into the vectors? Also specify exactly what you expect the output to be.

Reply With Quote
  #11  
Old March 5th, 2006, 08:21 PM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
this function insert strings in loop into the map from file
the "w_num" is counter that i use only to see changes in the input (if i'll insert "aaa" twich i'll want to see when i've insert that string (with the data in vector)

Code:
void	wordinfo::add_info(const string rword,const int w_num)
{
    mapi[rword].push_back(w_num);

    return;
}


now the problem is pull all the data - right now i tring lots of wierd ways to do it

Code:
void	wordinfo::printall()
{
    string	test;
    int	count = 0;

   for(iter = mapi.begin(); iter != mapi.end(); iter++)
    {
   
       test	 = iter->first;
       cout << iter->first;
		
       while(mapi[test].size() != 0)
         {
			
	cout <<" "<< mapi[test].front()<<endl;
	mapi[test].pop_back();

        }

	return;
}


i can't figure how move the pointer in the vector so i'll be able print all the vector values

thanks by the way

Reply With Quote
  #12  
Old March 5th, 2006, 08:32 PM
DardaDai DardaDai is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 11 DardaDai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 53 sec
Reputation Power: 0
found the problem

it was so simple and i just somhow missed it

it should be :

Code:
void	wordinfo::printall()
{
   string	test;
   int	count = 0;

   for(iter = mapi.begin(); iter != mapi.end(); iter++)
    {
        
        test	=	iter->first;
        cout << iter->first<<endl;
		
        for(count = 0;count < mapi[test].size();count++)
	{
	   cout<<iter->second[count]<<endl;	
	}	
    }

	return;
}


thanks again

Reply With Quote
  #13  
Old March 5th, 2006, 08:34 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
you can iterate through all STL containers in a similar way. you're already doing it for the map, now do it for the inner vector as well. change your loop thusly:
Code:
for (map<string, vector<int>, ltstr>::iterator miter = mapi.begin(); miter = mapi.end(); ++miter)
{
	cout << miter->first;
	for (vector<int>::iterator viter = miter->second.begin(); viter != miter->second.end(); ++viter)
	{
		cout << " " << *viter << endl;
	}
}

EDIT: whoops, you beat me. your way works too, but many people will consider the iterator route "cleaner". It really doesn't matter though.

Last edited by ubergeek : March 5th, 2006 at 08:36 PM. Reason: fatal typo!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need Help with STL (map)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |