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 April 10th, 2006, 09:42 AM
Randall311 Randall311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 9 Randall311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 0
Need help with map please!

I am trying to load a map with data being a vector of ints and the keys being int. I think I have it set up correctly, but I want to be able to print the map out to the screen to make sure it was initalized correctly. To do this I set up 2 loops. The first iterates through the map keys, the second is supposed to iterate through the data vectors. Here is the code that I came up with to test it. (It really doesn't like the second for loop at all.) Any help is greatly appreciated.

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

using namespace std;

int vals[] = { 10, 100, -30, 0, -50, 22 };
const size_t size1 = sizeof vals / sizeof vals[0];
vector<int> v(vals, vals + size1);

int vals2[] = { 54, 70, 87, 91, 82, 400, 69 };
const size_t size2 = sizeof vals2 / sizeof vals2[0];
vector<int> test(vals2, vals2 + size2);

map <int, vector<int> > vMap;
map <int, vector<int> >::const_iterator iter;
map <int, vector<int> >::const_iterator viter;

int main () 
{
	vMap[1] = test;
	vMap[2] = v;

	cout << "vector initialized with an array:\n";
	// display it, similar to the array
	for(int k = 0; k < v.size(); k++)
	{ 
		cout << setw(5) << v[k];
	}
	cout << endl;

	if(!vMap.empty())
		cout << "vMap has " << vMap.size() << " entries" << endl;
	else
		cout << "vMap is empty" << endl;

	for ( iter = vMap.begin(); iter != vMap.end(); ++iter ) {

		// this is the key
		cout << iter->first << '\t';

		// this is the data
		// iterate through the vector of ints for each key
		for ( viter = iter->second.begin(); viter != iter->second.end(); ++viter )
		{
			cout << *(viter) << ", ";
		}

		cout << endl;
	}



	return (0);
}

Reply With Quote
  #2  
Old April 10th, 2006, 09:54 AM
Randall311 Randall311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 9 Randall311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 0
never mind. I realized that my mistake was initalizing the vector iterator as a map rather then a vector. simply this line:
Code:
map<int>, vector<int> >::iterator viter;


shoud read:
Code:
vector<int>::iterator viter;


thanks to anyone who tried to help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help with map please!


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT