Map in C++ give the key and get the string [closed] - c++

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

Related

My recursive display function is not going to the next value of the array

I have a recursive display function, meant to go through the values of array b and print the details. My function is successful in looping the correct amount of times, but only prints out the value at index 0. For example, if I have 3 books, it prints out the first book 3 times and is not going to the other values. I am a beginner programmer and I believe I am missing something very simple or obvious, but any help is appreciated.
void displayBooks(int n)
{
// Write the code below
if (n >= currentCount) {
return;
}
else {
cout << "Name: " << b->getName() << "\n";
cout << "Name of Book Author: " << b->getAuthor() << "\n";
cout << "Publication Year: " << b->getYearOfPublication() << "\n";
cout << "ID: " << b->getID() << "\n";
displayBooks(n + 1);
}
}
This is the function itself, however I can't show the complete program since it is a lot of code with multiple files. When the function is first called as displayBooks(0) in a switch case.
I believe that you are not printing out each index of the "b" variable you need to access the index of each one. You need to have b as an array of pointers then access the index of that variable like b[n]->someProperty();
You can create the array like this:
Obj* b[HOWMANY];

How to fix problem with iterating through list if times using iterator? [closed]

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 4 years ago.
Improve this question
Debugger sais there is an incompatible iterator.
How i can solve this.
What can cause the problem.
Here is my main code
for (std::list<std::chrono::duration<double, std::milli>>
::iterator it = road.get_times().begin()
;it!= road.get_times().end();it++,z++) //error incompatible iterator
{
*road::file << "Samochod z drogi " << road.get_lane_number() << " pojechal ";
switch (road.get_direction())
{
case'S':
*road::file << "prosto" << std::endl;
break;
case'L':
*road::file << "w lewo" << std::endl;
break;
case'R':
*road::file << "w prawo" << std::endl;
break;
}
*road::file << "Jego czas stania w kolejce wyniosl ";
avarage_time += it->count() / 1000;
*road::file << round(it->count() / 1000) << std::endl;
}
function road.get_times ()
std::list< std::chrono::duration<double, std::milli>> get_times()
{
return times;
}
get_times returns by value, which means that every time you call it you get a new list object. This is what happens in your for loop. You call it 2 times and you get two objects. You can't compare iterators from two different objects. To fix this create one object by calling get_times just once:
auto times = road.get_times();
for (auto it = times.begin(); it != times.end(); ++it, z++)
//...
You also might want to pause and consider if returning by value is the right approach. I can't answer that for you since I don't know what times is and what is the structure of your program.

map.end() error "iterator not dereferenceable" c++ [duplicate]

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;

compare two vectors of string (character) [closed]

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

Why value of object's attribute returns zero after adding its pointer to a map in c++

So I have this:
//sons is an attribute of the object node that is a vector<Node*> that is initialized before
map<string,Node*> nodes;
string node_id = "node";
string son_id = "son";
Node *node = new Node(node_id, matrix, son_id, Prim);
cout << "Before " << node << endl;
cout << "Value of sons before map: " << node->sons[0] << endl;
nodes[node_id] = node;
cout << "After: " << nodes.find(node_id)->second << endl;
cout << "Value of sons after map: " << nodes.find(node_id)->second->sons[0];
I'm getting this output (with varying memory positions from execution to execution):
Before: 0x9dfdda8
Value of sons before map: 0xbff1a774 // consistant with memory position with created obj
After: 0x9dfdda8
Value of sons after map: 0
Why is this happening and how can I fix it?! I've been searching for the solution and trying to figure this out for 4 hours now...
cout << "After: " << nodes.find(root_id)->second << endl;
cout << "Value of sons after map: " << nos.find(root_id)->second->sons[0];
Why is the second line refering to nos and the first one nodes? Is it just a typo?
If those are really different objects, that might explain why you see inconsisten results
You are adding node to the map with the key node_id and then looking it up later with root_id and not even checking if it found one, so you're probably getting some undefined behaviour by accessing end(nodes). You need to access the map with the same key to get the same object.
Also you appear to have some confusion with your variables, using no and nos when you are apparently needing to access either node or the map's variable.