Elliot Weil
Elliot Weil

Reputation: 193

C++ error when inserting a pair into vector of maps

I'm trying to create a graph using a vector of maps. I am actually looking at the code from a book and trying to enter it into visual studios 2012 so I can mess around with the graphs. But for some reason it will not allow me to add a pair to vector. code below

creating the vector

//vector that holds a map of all adjacent vertices
vector<map<int, int> > adjList;

constructor for the graph class

Graph::Graph(int n){
    map<int, int> element;
    adjList.assign(n, element);
}

adding items into the vector

int v1 = e.v1;
int v2 = e.v2;
int weight = e.weight;
//add the first vertix the edge connects to intto the adjList
adjList.insert(make_pair(v1, weight));
//add the second vertix the edge connects to into the adjList
adjList.insert(make_pair(v2, weight));

errors I'm getting from visual studios 2012 when trying to compile

Error   1   error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments  c:\users\elliot\documents\visual studio 2012\projects\graph\graph.cpp   25  1   Project1
Error   2   error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments  c:\users\elliot\documents\visual studio 2012\projects\graph\graph.cpp   27  1   Project1

Upvotes: 1

Views: 1887

Answers (2)

darxsys
darxsys

Reputation: 1570

I thought I could make it clear in the comments, but let's make it more detailed. You have a vector of maps. You are trying to insert a pair of some values into a vector of maps. That is, of course, not possible (this ain't python). What you should and could do is something like this:

adjList[0].insert(make_pair(v1, weight));

or any other index you need to insert something at.

Check this out.

What I'm guessing is the following. Your every node is a number (it's id is an integer). So, with that number, you index a vector and get it's adjacency list. Adjacency list is a map. Each entry in the map is id of another neighbour and probably the length of the edge. So for example, if you want neighbours of node with ID 3, you would ask for adjList[2] (they are probably indexed from 0) and get a map of it's neighbours.

Upvotes: 2

user2498534
user2498534

Reputation: 76

The insert member function takes two parameters: a position, and a value. If you don't care about specifying a position, then just use the push_back function. You may have other issues, such as type issues, but this is your immediate problem.

You shouldn't assume that you compiler is speaking gibberish. It told you exactly what was wrong:

no overloaded function takes 1 arguments

Take a quick trip to a handy reference to see that it was right:

iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, size_type n, const value_type& val);  
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last); 
iterator insert (const_iterator position, value_type&& val);
iterator insert (const_iterator position, initializer_list<value_type> il);

Upvotes: 0

Related Questions