
April 10th, 2006, 09:42 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 9
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);
}
|