This question already has answers here:
iterators can't access problems properly
(3 answers)
Closed 5 years ago.
//Works
cout << "map[0] value is " << doubleStatsMap.begin()->first<< endl;
//gives error
cout << "map[last value is " << doubleStatsMap.end()->first << endl;
Im simply trying to get the value of the last element of my map. It works correctly with
"map.begin->first"
but is giving "map/set iterator not dereferencable" for the
"map.end()->first".
It cant be empty as the map has a beginning thus it has an end. Everything I've read says this should work. Any suggestions greatly appreciated!
Trying to get anything from the end iterator causes undefined behavior.
To get the last item, you can use std::map::rbegin().
// cout << "map[last value is " << doubleStatsMap.end()->first << endl;
cout << "map[last value is " << doubleStatsMap.rbegin()->first << endl;
Related
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 5 years ago.
Improve this question
I have created a map like this : std::map<int, std::string> mapID;
INPUT: mapID[1] = "string!";
and I want to print element with the key is the s.
For example
cout << "The string is : " << mapID.at(s)->second << endl;
OR
cout << "The string is : " << mapID.find(s)->second << endl;
But I have the error
error: no matching function for call to ‘std::map >::at(std::string&)’
UPDATE:
while(getline(file,s))
{
cout<< s << endl;
number = atoi(s.c_str());
cout << "The string is: " << mapID.at(number)<< endl;
}
If you look at the reference http://en.cppreference.com/w/cpp/container/map/at you will see, that method "at" returns a second element of pair. In your case it is std::string.
The code should be
cout << "The string is : " << mapID.at(1) << endl;
Since C++11, .at() returns a reference to the mapped value of the element identified with key k.
Your example was good if you used an iterator as you can then access your pair element made by your map.
Here is an example with an iterator
auto it = mapID.find(1);
cout << "The string is : " << it->second << endl;
OR with .at()
cout << "The string is : " << mapID.at(1) << endl;
But beware that .at() will throw if the element is not found in your map as it can't create it like the [] operator
This question already has answers here:
No console output on cout
(7 answers)
Closed 5 years ago.
Is this possible to print Point value?
For the example, I have a Point like this:
Point coordinate = Point(150,300);
And I want to show its value. I tried several ways, like:
first:
cout << coordinate << "\n";
second:
cout << coordinate.x << coordinate.y << "\n";
I also try the suggestion to flush it, become:
std::cout << coordinate << std::endl;
But none of those are work in my case. Is there any suggestion? Thanks for your help.
Ps. I work with opencv 3 and c++
std::cout << coordinate.x << "," << coordinate.y << std::endl;
I have the following code, which is a simplified version of what I'm trying to do.
std::vector<GLuint> testVector = { 6, 7, 8 };
std::map<std::pair<GLfloat, GLfloat>, std::vector<GLuint>> testMap;
testMap.insert(std::make_pair(std::make_pair(1.0f,1.0f), testVector));
std::vector <GLuint> retrievalVector =
testMap.find(std::make_pair(1.0f, 1.0f))->second;
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << std::endl;
retrievalVector.push_back(9);
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << "\t"
<< retrievalVector[3] << std::endl;
testMap.insert(std::make_pair(std::make_pair(1.0f, 1.0f), retrievalVector));
retrievalVector = testMap.find(std::make_pair(1.0f, 1.0f))->second;
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << "\t"
<< retrievalVector[3] << std::endl;
Basically, I insert a vector of integers into a map using coordinates as a key. I retrieve the same vector and find that the contents were stored successfully. I then add another integer to the vector and reinsert it in the same place. Retrieving it once more and attempting to print the vector's contents (the last cout) leads to an out of range access.
Can someone who is more knowledgeable explain why this occurs?
The problem is that your second insert fails, as you already have a value at that position. If you want to modify the value, you have to do something like testMap[std::make_pair(1.0f, 1.0f)] = retrievalVector;.
The behaviour is exactly as expected. According to the documentation:
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
You already have a std::make_pair(1.0f, 1.0f) in there, so your return value should be:
Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
If you check testMap.insert(std::make_pair(std::make_pair(1.0f, 1.0f), retrievalVector)).second it should be false, so there is still a three-element array there, not the four-element one you hoped had been added, therefore the program crashes on trying to access retrievalVector[3].
By the way, C++17 has an insert_or_assign() function that will do what you hoped insert() would do.
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 6 years ago.
Improve this question
I want to compare two vectors string
vector <string> morse ={"A.-","B-...","C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I.." ,"J.---", "K-.-", "L.-..", "M--" ,"N-." ,"O---" ,"P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--" ,"X-..-" ,"Y-.--", "Z--.."};
vector<string> codeMorse (1);
codeMorse ={".---.--.-.-.-.---...-.---."};
if (morse[i][j]==codeMorse[k]){ //my problem here =error
}
Can anybody help me?
Your code has 2 problems:
You can't make 2-Dimensional vector that way nor you even tried to make it 2D.
You wrote morse[i][j] without previous definition of i and j.
To fix issue 1 & 2:
Include
#include <vector>
Make a vector of std::pair(s):
std::vector<std::pair<std::string, std::string>> morse;
That allows you to have a pair of strings.
To add a new morse code, use this:
morse.push_back(std::pair<std::string, std::string>("LETTER HERE", "MORSE CODE HERE"));
To read 'em use this:
//read all via loop
for (int i = 0; i <= morse.size(); i++) {
std::cout << "Letter: " << morse[i].first << std::endl; //.first access your first elemt of the pair
std::cout << "Morse Code: " << morse[i].second << std::endl; //.second to access the morse code
}
OR use iterators if you already know them:
//read all via loop
for (auto i = morse.begin(); i != morse.end(); i++) {
std::cout << "Letter: " << i->first << std::endl; //->first access your first elemt of the pair
std::cout << "Morse Code: " << i->second << std::endl; //->second to access the morse code
}
Of course you can read specific values:
std::cout << morse[0].first << std::endl; //[] same use as the array's brackets
std::cout << morse[0].second << std::endl; //same here
This question already has answers here:
unable to print char* pointing to 0
(4 answers)
Closed 9 years ago.
In Visual studio 2012, I was messing around with pointers, and I realized that this program kept crashing:
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const char* pointer = nullptr;
cout << "This is the value of pointer " << pointer << "." << endl;
return 0;
}
My intent was the set a pointer to nullptr, and then print the address. Even though the program compiles, it crashes during runtime. Can someone explain what is going on?
Also, what's the difference between pointer and *pointer?
You are using a const char* which, when used in std::cout's operator <<, is interpreted as a string.
Cast it to void* to see the pointer's value (the address it contains):
cout << "This the value of pointer " << (void*)pointer << "." << endl;
Or if you want to be pedantic:
cout << "This the value of pointer " << static_cast<void*>(pointer) << "." << endl;
LIVE EXAMPLE
Although you can do cout << "Pointer is " << (void*)pointer << ".\n"; as already been suggested I feel that in this case the "C way" of doing it is prettier (no casting) and more readable: printf("Pointer is %p\n",pointer);