Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a C++ program using iterators. I have a data structure that is a map. I use iterators to loop from the begin to the end of the map and for each element of the map I do something with the key and the value.
So when I want to know the key and the value of a specific element of the map, I use first() and second() on my iterator.
Like this :
#include <map>
#include <pair>
map<unsigned long, int> myMap;
map<unsigned long, int>::const_iterator it;
for(it = myMap.cbegin(); it != myMap.cend(); ++it)
{
unsigned long key_of_map = it.first();
int val = it.second();
cout << "Key is : " << key_of_map << endl << "Value is : " << val << endl;
}
When I compile it, it tells me :
"../src/myfile.cpp:16:10: fatal error: 'pair' file not found"
I'm using Eclipse (version Luna), the standard version I downloaded from the official website (I didn't changed anything).
You need to
#include <utility>
to use std::pair<>
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Trying to create a struct that has a vector as a member, but unable to make it work if vector has multiple args.
Any ideas?
struct node {
int id;
std::string name;
bool visited;
int state;
std::vector <std::string, int> edges;
};
I'd like the vector to be of this type; std::vector <std::string, int> string and int.
This is the error:
/usr/local/Cellar/gcc/11.3.0/include/c++/11/bits/stl_vector.h:477:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<std::__cxx11::basic_string<char>, int>::_Base'
477 | using _Base::_M_get_Tp_allocator;
std::vector holds a sequence of a single type.
e.g. std::vector<int> v_int holds a sequence of only integers.
There are types that can themselves hold multiple types. For example, a std::pair<std::string, int> holds both a string and an int.
It is possible to have a vector of pairs, if that is what you are trying to do.
std::vector<std::pair<std::string, int>> edges;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have an array of size 80, std::array< std::optinal<unsigned int>, 80> myArray which contains numbers from 0-9.
I would like to get the index of the first array element with value x=5.
When I do as following,
auto it = std::find(std::begin(myArray), std::end(myArray), x);
auto index_of_x =std::distance(myArray, it);
I get the following error,
error: no matching function for call to ‘distance(std::array< std::optinal<unsigned int>, 80>&, const std::optional<unsigned int>*&)’
93 | auto index_of_x =std::distance(myArray, it);
| ^
What is the issue here? Is it a problem with using std::optional?
std::distance expects 2 iterators, not a container and an iterator.
Call should be:
auto index_of_x = std::distance(std::begin(myArray), it);
First parameter to std::distance is iterator pointing to the first element. Try
std::distance(myArray.begin(), it);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I keep getting an error while running this code. I've searched for a solution for similar errors, but as a junior programmer, I can't understand solutions meant to be for different codes.
Here is my code. Please give me a solution with demonstration.
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main() {
vector <int> listed_elements {10,6,11};
auto min_value = min_element(listed_elements.begin(), listed_elements.end());
cout << "The smallest element on the list is: ";
cout << min_value << endl;
return 0;
}
Iterators mimick pointers. You can dereference the iterator to get the element:
cout << *min_value << endl;
Note that in general algorithms often return end (the iterator you passed as second parameter) to signal that no element was found and then you need to check that you have a valid iterator before dereferencing it:
if (min_value != listed_elements.end()) {
std::cout << *min_value << std::endl;
}
However, the only case where std::min_element returns the end iterator is when the range is empty, so in case you are absolutely certain that this isn't the case you can drop that check (but only then!).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I need help using unsigned chars in std::vectors that are inside of a std::map.
This is how I declare the std::map:
std::map<int, std::vector<unsigned char>> DataMap;
The problem comes when I try to assign a std::vector to the std::map.
it->first comes from another std::map, as this code is inside a loop.
std::vector<unsigned char> charHolder;
for(int i = 0; i < 10; i++)
{
charHolder.push_back('2');
}
DataMap.insert(std::pair<int, std::vector<unsigned char>(it->first, charHolder));
The errors:
Template argument 2 is invalid
I need to assigned a char[] array to the 2 place in the std::map. I've tried an array, but I had no luck.
You are missing a > character
DataMap.insert (std::pair<int, std::vector<unsigned char>>(it->first, charHolder));
^
You may use uniform initializer as following:
DataMap.insert ({it->first, charHolder});
Some of the many fun and varied ways to get data into a map:
std::map<int, std::vector<unsigned char>> DataMap;
void add(int i, std::vector<unsigned char> v)
{
// efficient move versions
DataMap.emplace(i, std::move(v));
DataMap[i] = std::move(v);
DataMap.insert(std::make_pair(i, std::move(v)));
DataMap.emplace(std::piecewise_construct,
std::make_tuple(i),
std::forward_as_tuple(std::move(v)));
// less efficient copy versions
DataMap.emplace(i, v);
DataMap[i] = v;
DataMap.insert(std::make_pair(i, v));
DataMap.emplace(std::piecewise_construct,
std::make_tuple(i),
std::make_tuple(v));
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef std::vector<int> VectorInt;
typedef std::map<int, VectorInt> MapVectorInt;
int main() {
MapVectorInt myMap;
VectorInt v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
VectorInt v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
myMap.insert(std::make_pair(0, v1));
myMap.insert(std::make_pair(1, v2));
return 0;
}
What is the bese way to take (extract/get) the vector at position x (eg 1,2...);
You can use:
auto v1=myMap[0]; //This will do a deep copy for the vector
or:
auto& v1=myMap[0]; //This will hold a reference to the vector
Be careful in the second approach because if the item has removed , the reference will be invalid.
If you want a safe option use std::map::at:
auto v1=myMap.at(0);
or:
auto& v1=myMap.at(0)
This will throw an exception (std::out_of_range) if the item is not there.
Short answer:
Best place to look for it http://en.cppreference.com/w/cpp/container/map/find
With context:
For visualisation only, I guess you should express you intention as you said in your question(get an element from the map and not "eventually" insert it as using [] for accessing a non-existing element creates it), so:
const auto& search = myMap.find(1);
if (search != example.end()) {
for(auto value: search->second) {
std::cout << value << '\n';
}
}
else {
std::cout << "Not found\n";
}
Try It Live
You can refer to the vector in the map with myMap[0] or myMap[1].
You could make an alias for either of those, e.g. auto& x = myMap[0];. Using the alias will access the vector as it is stored in the map.
To take a copy of the vector (so that you can modify the map contents without changing the copy), it is just the same as copying any other object:
auto x = myMap[0];
Note that 0 and 1 refer to the key you used in the insert call; not to the search order or the insertion order (although in your example those things all happen to be the same).