I init a vector like this:
vector<pair<float,int>> A(make_pair(1.0,0),10);
and error occured:
error:no matching function for call to ‘std::vector<std::pair<float, int> >::vector(std::pair<float, int>, int)’
so how to init vector<pair> struct? I want init this with N same pairs, should i use push_back?
You have a vector of pairs. To construct one with ten pairs, you need to specify the amount first, then the initial values. In other words: "How much of what":
vector<pair<float, int>> A(10, make_pair(1.0f, 0));
Related
I want to initialize a vector of integer pairs while specifying its size (I have to use this structure). I tried:
vector<pair<int, int>> container;
container.emplace_back(size);
And:
container.emplace_back(size, make_pair(0, 0));
But I keep having this error:
error: no matching function for call to 'std::pair<int, int>::pair(long long unsigned int&, std::pair<int, int>)'
Is there any solution or different approach?
Thank you!
emplace_back forwards its parameters to the elements constructor. std::pair<int,int> has no constructor that takes a size and a pair, hence the error. To emplace an element:
std::vector<std::pair<int, int>> container;
container.emplace_back(0,0);
However, if you want to construct a vector of certain size upfront, you need not emplace elements, because they are already there:
std::vector<std::pair<int, int>> container(size);
container[42] = make_pair(1,2); // 42 < size !
I'm guessing what you really want is something like:
vector<pair<int, int>> container(size);
This will initialize the vector constainer with size number of default-constructed elements.
I'd like to sort a map<pair<string, int>, int> dbg; by value using lambda :
For this I have
void test()
{
map<pair<string, int>, int> dbg;
sort( dbg.begin(), dbg.end(),
[]( pair<pair<string, int>, int>& lht, pair<pair<string, int>, int>& rht) {
return lht.second > rht.second;
});
}
But compilation failed with a lot of errors. What is the right lamda prototype here?
Sorting a map is nonsensical; it's already sorted, and you can't change the sort order after the fact by sorting it (the order can't be changed at all except by adding and removing elements, and they'll always fall into a fixed order). If you want to sort it in a different way, either:
Provide the alternate comparator to map so it's naturally sorted the way you want, or
Copy the entries to sequence type (e.g. vector) and sort that.
In this case, you want to sort by the value, which is not possible for a map, so option #2 is your only option.
I'm trying to make an array of pairs of vectors, and in the vectors is another pair of int and float. Here is the code to help explain: pair<vector<pair<int,float> >, vector<float> >[numNodes];
At first I just had an array of a vector of pairs, but now that I changed it to an array of pairs I'm being thrown errors. I think the errors have to do with the placement of make_pair but I'm not sure.
Here is my code, but first, I little more information on what exactly I'm trying to do with the code. I'm working on a school assignment where the goal is to read in a three files of a graph and store it as an adjacency matrix and adjacency list. Right now I'm working on the list. One file has just the connections, another has the weights of the connections, and the last file has the position values of each node. Then I will search the graphs with different search algorithms. The array of vectors was the adjacency list and all the other data types involved in the pair<vector<pair<int,float> >, vector<float> >[numNodes]; were an attempt to store these variables in the list. I know another way of doing this would be to have a Node object with member variables where I can save the nodeID and position values, but I don't know exactly how this would work in terms of adding it into the list. Also would there be advantages to doing this rather than having everything stored in the list?
class AdjacencyList{
public:
pair<vector<pair<int,float> >, vector<float> > *adjList;
int numNodes;
AdjacencyList(int numNodes){//constructor
this->numNodes = numNodes;
adjList = new pair<vector<pair<int,float> >, vector<float> >[numNodes];
}
void addEdge(int sourceNode, int destNode, float weight, vector<float> posVals){
make_pair(adjList[sourceNode].push_back(make_pair(destNode, weight)), posVals);
}
}
And here is the error I'm being thrown:
main.cpp:71:39: error: no member named 'push_back' in
'std::__1::pair<std::__1::vector<std::__1::pair<int, float>,
std::__1::allocator<std::__1::pair<int, float> > >,
std::__1::vector<float, std::__1::allocator<float> > >'
make_pair(adjList[sourceNode].push_back(make_pair(destNode, weig...
Another Error I'm getting after changing the array of vectors to an array of pairs is this:
no member named 'size' in
'std::__1::pair<std::__1::vector<std::__1::pair<int, float>,
std::__1::allocator<std::__1::pair<int, float> > >,
std::__1::vector<float, std::__1::allocator<float> > >'
return adjList->size();
This size function was working before the change, which makes me think these errors are happening because its recognizing it as a pair now instead of an array. Any ideas on how to fix this?
I have no idea what vector of pairs of vectors of pairs of ints and floats and vectors of floats could possibly in any possible way represent, but anyway you could just assign to the pairs of vectors you want to assign to:
#include <vector>
class AdjacencyList{
public:
std::vector<
std::pair<
std::vector<std::pair<int,float>>,
std::vector<float>
>
> adjList;
int numNodes;
AdjacencyList(int numNodes) :
numNodes(numNodes),
// construct the vector with numNodes default-constructred elements
adjList(numNodes) {
}
void addEdge(int sourceNode, int destNode, float weight, std::vector<float> posVals){
// prefer at() instead of [] for error checking
adjList.at(sourceNode) = std::make_pair(
std::vector<std::pair<int, float>>{
// vector of pairs?? anyway:
std::make_pair(
destNode, weight
)
},
posVals
);
}
};
Do not use raw pointers - use std::vector for managing dynamic array. Your code leaks memory allocated by new.
In the first error thrown, the compiler is complaining about a missing method push_back being called. If you look at it, this makes sense because you are trying to call push_back from a pair variable (which does not have implemented any push_back method). And a similar thing goes with the second error: size() is a valid method for the vector class, but here you are calling it from a pointer pointing to an array (check this question for more info).
Instead of an array of pairs, you could use a vector of pairs, or even a set of pairs:
class AdjacencyList{
public:
vector<pair<vector<pair<int,float> >, vector<float> > > adjList;
int numNodes;
AdjacencyList(int numNodes){//constructor
this->numNodes = numNodes;
// no need to initialize the vector
//adjList = new pair<vector<pair<int,float> >, vector<float> >[numNodes];
}
void addEdge(int sourceNode, int destNode, float weight, vector<float> posVals){
// add the new pair <int, float> to the existent vector for this source node
adjList[sourceNode].first.push_back(make_pair(destNode, weight));
// define second value of the outer pair as the parameter variable posVals?
adjList[sourceNode].second = posVals;
// or you wanted to do this? I don't think so :S
make_pair(adjList[sourceNode].first, posVals);
}
}
You could do the same using an outer set instead of vector, and then you would need to change the push_back(...) for insert(...), so that you obtain the benefits of sets of pairs as well.
Actually, if you need to do it using outer arrays, then your code would be fine but changing your AddEdge function to the one proposed above.
I don't really know which is the desired performance of the code, but if you provide more information about it I might be able to help a bit more.
I think you have some inconsistencies in the type declared and the type being added.
You have declared an array (now does not matter whether it is an array, vector, or set) of pairs of vectors. Whereas on your addEdge function you are creating a pair of None (push_back returns None) and vector. The push_back on its side is adding the pair <int, float> of the first vector of the outer pair to you differently variable defined above.
In c++, I would like to insert a vector in a map.
The key of the map is a pair of string and int, and the value of one is a vector.
I am writing down the following code, however it seems that the vector is not inserted into the map.
Is the syntax of the code is wrong?
If so, could you tell me correct one?
map<pair<string, int>, vector<string> > my_map;
vector<string> v;
v.push_back("abcde");
my_map.insert(make_pair(make_pair("aaa",1),v));
You have used the v_pre while vector is of name v:
my_map.insert(make_pair(make_pair("aaa",1),v_pre));
The correct code should be:
my_map.insert(make_pair(make_pair("aaa",1),v));
I'm trying to implement a 2D unordered_map that looks like:
std::unordered_map<std::string, std::unordered_map<std::string, double>>
So first, I implemented the inner unordered_graph by doing:
std::unordered_map<std::string, std::unordered_map<std::string, double> *inner = new
std::unordered_map<std::string, std::unordered_map<std::string, double>>()
inner->insert(std::make_pair("X", 0));
Then, I tried to make the outer unordered_map by doing
std::unordered_map<std::string, std::unordered_map<std::string, double> *outer =
std::unordered_map<std::string, std::unordered_map<std::string, double>>()
outer->insert("X", inner);
but it gives me an error saying thatno matching function for call to insert
You're using insert wrong here:
outer->insert("X", inner);
It expects a value_type i.e. std::pair. You're passing two arguments instead of one, so you need to do make_pair() on those arguments, plus you need to pass a value, so *inner instead of inner which is a pointer.
Once this is all said and done, you will probably be better off with a different data structure, as a hash table of hash tables is usually not the most efficient.