I have a hw question that asks us to create an STL funciton that takes an argument of type map<string, int> and an integer and returns vector containin all positions in map for which the integer value is found.
The function works from what I can test. But I wanto be able to test it in main without outputting the results from within the function itself. Can someone show me how I'm supposed to test the function?
I commented the code I tried but didn't work.
working code:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
vector<map<string,int>::iterator>val_count( map<string,int>&m, int val);
void main() {
int n;
map<string,int> m_vals;
// vector<map<string,int>::iterator> positions;
m_vals["one"] = 1;
m_vals["two"] = 2;
m_vals["three"] = 3;
m_vals["four"] = 2;
m_vals["five"] = 5;
m_vals["six"] = 2;
//positions = val_count (m_vals, 2);
val_count (m_vals,2);
//copy (positions.begin(), positions.end(), ostream_iterator<int>(cout," "));
cout << endl;
cin >> n;
cout << endl;
}
vector<map<string,int>::iterator>val_count( map<string,int>&m, int val) {
vector<map<string,int>::iterator> v_found;
map<string,int>::iterator pos;
for (pos = m.begin(); pos != m.end(); ++pos) {
if (pos->second == val) {
//currently using this to test results of function
cout << pos->first << endl;
v_found.push_back(pos);
}
}
return v_found;
}
thank you in advance