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

Make the function val count return a vector of integers by making v_found as a vector of integers (vector<int>). Alternatly you can pass the vector reference to the function, and make the function modify the original vector. Print the returned vector or the modified vector in the main function. Something like:

size_t size = v_found.size();
for(size_t i = 0; i < size; ++i)
{
    cout << v_found[i] << '\n';
}


This
would be a good tutorial for you.

The problem is I can't modify the prototype of the function. Is this to say that there is no other way of being able to print in main?

Thanks,

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

typedef map<string,int> simap ;
typedef simap::iterator miterator ;
typedef vector<miterator> ivector ;

ivector find_values( simap& m, int val ) ;

int main()
{
    simap map ;
    map["one"] = 1;
    map["two"] = 2;
    map["three"] = 3;
    map["four"] = 2;
    map["five"] = 5;
    map["six"] = 2;
    
    ivector found = find_values(map,2);
    for( ivector::size_type i=0U ; i<found.size() ; ++i )
      cout << found[i]->first << ' ' << found[i]->second << '\t' ;
    cout << '\n';
}

ivector find_values( simap& m, int val )
{
    ivector v_found;
    for( miterator i = m.begin() ; i != m.end() ; ++i )
        if( i->second == val ) v_found.push_back(i) ;
    return v_found;
}

note: the question is not const-correct. ideally, it should have taken a reference to a const map, and returned a vector of const_iterators. it appears inefficient to make a copy of the vector on return from the function, but a good compiler can use RV optimization to avoid the overhead.

it is supposed to pass a reference to const map which I fixed here. but i'm still not understanding how Im' supposed to print out in main. I'll have to review your code more in depth.

here's my new code with vijayan's modifictaions and mine, but now it won't output:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

typedef map<string,int> si_map ;
typedef si_map::iterator m_iterator ;
typedef vector<m_iterator> it_vector ;

it_vector val_count(const si_map &m, int val);

void main() {
    int n;

    si_map m_vals;
    it_vector found;

    m_vals["one"] = 1;
    m_vals["two"] = 2;
    m_vals["three"] = 3;
    m_vals["four"] = 2;
    m_vals["five"] = 5;
    m_vals["six"] = 2;
    
    found = val_count (m_vals,2);

    for( it_vector::size_type i=0U ; i < found.size() ; ++i )
        cout << found[i]->first << ' ' << found[i]->second << '\t' ;
    
    cout << endl;

    cout << endl;
    cin >> n;
    cout << endl;
}

it_vector val_count(const si_map &m, int val) {

    si_map m_temp(m);
    m_iterator pos;
    it_vector v_found;

    for (pos = m_temp.begin(); pos != m_temp.end(); ++pos) {
        if (pos->second == val) v_found.push_back(pos);
    }

    return v_found;
}

now it will not compile. for a const map, the iterator is a const_iterator. change the typedef for the iterator to

typedef simap::const_iterator miterator ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.