Getting backwards output when iterating through a map and printing content - c++

When I read through the code posted below it appears that the output printed would be:
Syndy
James
Phuong
Germaine
Agatha
Anges
Jack
However, when I run it I get Germaine and his vector of friends printed first, then Syndy and her vector of friends second:
Germaine
Agatha
Anges
Jack
Syndy
James
Phuong
Can anyone help me understand why Germaine, the second key, is accessed and printed first in the printFacebook function instead of Syndy?
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
void printFacebook(map<string, vector<string>>& m) {
for (pair<string, vector<string>> p : m) {
cout << p.first << endl;
for (string f : p.second) {
cout << "\t" << f << endl;
}
}
}
int main() {
map<string, vector<string>> facebook;
facebook["Syndy"].push_back("James");
facebook.find("Syndy")->second.push_back("Phuong");
//map<string, vector<string>>::iterator f = facebook.find("Syndy");
//auto f = facebook.find("Syndy");
facebook["Germaine"];
facebook.find("Germaine")->second.push_back("Agatha");
facebook.find("Germaine")->second.push_back("Anges");
facebook.find("Germaine")->second.push_back("Jack");
printFacebook(facebook);
}

A std::map stores its items ordered by the value of the keys. In your case, the keys are "Syndy" and "Germaine". The default ordering of those is such that "Germaine" is first and "Syndy" is second.
Hence, when you iterate over the items of the map, the item corresponding to the key "Germaine" is seen before the item corresponding to the key "Syndy".
That explains the output.

Related

Composite map: take data from another Map

I need to take the occurrences of words taken from a file, using map<string,int>, and then I need to copy them to a map<int,
vector<string>, cmpDec >, and print them in decreasing order.
I tried to take word frequencies from a file to a map<string, int> and then I'm trying to copy it to a map<int,
vector<string> > with no results
I have declared 2 maps:
map<string, int> text;
map<int, vector<string>, cmpDec> freq;
I take the text from a file in the first map with the word frequencies:
while (rf >> words) {
text[words]++;
}
Now I have to put the frequencies in the second map (required), where I need to have first int, for num of word frequencies, vector with the words for each freq, and the compare for decreasing frequencies.
Now i'm trying to put the datas in the second map from the first in these ways:
map<string, int>::iterator iter_map1 = text.begin();
map<int, vector<string>>::iterator iter = freq.begin();
vector<string>::iterator iter_v;
for (; iter_map1 != text.end(); ++iter_map1) {
iter->first.insert(make_pair(iter_map1->second, iter->second.push_back(iter_map1->first)));
}
It gives 2 errors on the iter->second.... line:
...\BagOfWords.cpp|56|error: request for member 'insert' in 'iter.std::_Rb_tree_iterator<_Tp>::operator-><std::pair<const int, std::vector<std::__cxx11::basic_string<char> > > >()->std::pair<const int, std::vector<std::__cxx11::basic_string<char> > >::first', which is of non-class type 'const int'|
and
...\BagOfWords.cpp|56|error: invalid use of void expression|
What am I doing wrong? Is there an easier way to take words (and their frequencies) from a file and put them on the second map without passing from the first?
With C++17 you can do structured binding, which helps a lot when iterating through a map.
#include <map>
#include <vector>
#include <string>
#include <iostream>
using WordCounts = std::map<std::string, int>;
using FrequencyOfWords = std::map<int, std::vector<std::string>, std::greater<int>>;
int main()
{
WordCounts word_counts;
FrequencyOfWords words_freq;
std::vector<std::string> words = {"test", "hello", "test", "hello", "word"};
for(const auto& word : words)
word_counts[word]++;
for(const auto& [word, count] : word_counts)
words_freq[count].push_back(word);
for (const auto& [freq, words] : words_freq)
{
std::cout << "freq " << freq << " words";
for (auto const& word: words)
std::cout << " " << word;
std::cout << '\n';
}
}
I don't think that you can do this in one pass as you don't know the word counts upfront.
First, a couple of recommendations. Use typedef (or using for C++ 11 or later). This will save you some typing and also ensure that your types are correct. In your code freq and iter don't have the same underlying container type (they differ in the comparison used).
Secondly, try to use the standard library as much as possible. You don't show cmpDec but I guess that it is a comparator based on greater-than rather than the default less-than. I would prefer to see std::greater<int> rather than a custom comparator.
For your errors, in the line
iter->first.insert(...
iter is at the start of freq and you are trying to insert to first which is int.
This should probably be something like
freq[iter_map1->second].push_back(iter_map1->first);
Breaking that down
freq[iter_map1->second] This uses the int word count from text to lookup an entry in freq. If there is no entry an empty one will be inserted to freq.
.push_back(iter_map1->first) This inserts the string from text to the vector that was found or created in the previous step
Here is a full example of what I think you are trying to achieve.
#include <map>
#include <vector>
#include <string>
#include <functional>
#include <fstream>
#include <iostream>
using std::map;
using std::vector;
using std::string;
using std::greater;
using std::ifstream;
using std::cout;
using WordCounts = map<string, int>;
using FrequencyOfWords = map<int, vector<string>, greater<int>>;
int main()
{
WordCounts text;
FrequencyOfWords freq;
ifstream rf("so26.cpp");
string words;
while (rf >> words)
{
text[words]++;
}
WordCounts::const_iterator iter_map1 = text.begin();
for (; iter_map1 != text.end(); ++iter_map1)
{
freq[iter_map1->second].push_back(iter_map1->first);
}
for (auto const& e: freq)
{
cout << "freq " << e.first << " words";
for (auto const& w: e.second)
{
cout << " " << w;
}
cout << "\n";
}
}
Perhaps I misunderstood the question, but I reckon the following does what you want (I prefer unordered maps, as they are faster and you don't seem to need the ordering)
std::unordered_map<std::string,int> word_counts;
std::string word;
while(input >> word)
word_counts[word]++;
std::unordered_map<int,std::vector<std::string>> words_by_freq;
for(const auto& counted : word_counts)
words_by_freq[counted::second].push_back(counted::first);

Efficient way to get key from value when map contain vector of string as value

How to get key using value which is vector of string and vice versa. Below is my code.
#include<iostream>
#include<map>
#include<string>
#include <unordered_map>
#include <vector>
using namespace std;
int main()
{
std::unordered_map<std::string, std::vector<std::string>> Mymap;
Mymap["unique1"] = {"hello", "world"};
Mymap["unique2"] = {"goodbye", "goodmorning", "world"};
Mymap["unique3"] = {"sun", "mon", "tue"};
for(auto && pair : Mymap) {
for(auto && value : pair.second) {
std::cout << pair.first<<" " << value<<"\n";
if(value == "goodmorning") // how get key i.e unique2 ?
}}
}
case 1: When value is input. key is output.
Input : goodmorning
output : unique2
case 2: When key is input value is output.
Input : unique3
output: sun ,mon ,tue
Note : No boost library available.
For case 1, a combination of find_if and any_of will do the job.
For case 2, you can simply use the find method of unordered_map.
#include<iostream>
#include<map>
#include<string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
unordered_map<string, vector<string>> Mymap;
Mymap["unique1"] = { "hello", "world" };
Mymap["unique2"] = { "goodbye", "goodmorning", "world" };
Mymap["unique3"] = { "sun", "mon", "tue" };
// Case 1
string test_value = "goodmorning";
auto iter1 = find_if(Mymap.begin(), Mymap.end(),
[&test_value](const decltype(*Mymap.begin()) &pair)
{
return any_of(pair.second.begin(), pair.second.end(), [&test_value](const string& str) { return str == test_value; });
});
if (iter1 != Mymap.end())
{
cout << "Key: " << iter1->first << endl;
}
else
{
cout << "No key found for " << test_value;
}
// Case 2
test_value = "unique3";
auto iter2 = Mymap.find(test_value);
if (iter2 != Mymap.end())
{
int first = true;
for (auto v : iter2->second)
{
cout << (first ? "" : ", ") << v;
first = false;
}
cout << endl;
}
else
{
cout << "No value found for key " << test_value << endl;
}
return 0;
}
The key is stored in pair.first. Just use that if your use-case is in loop iteration as you illustrated.
If you mean in any use, without iteration, that is, given a value obtain the associated key, there is not a direct way to do that. You could build inverse maps for each value to key but that would not be really efficient considering also the fact that you would also need unique values.
Create another map going the other way for every vector entry?
If the array entries are not unique, then you would need to do the same map-to-vector, or use multimap.
Also consider using hash map (unordered_map), and stringview as ways to reduce the memory usage of the second map?
But the best answer would be the boost 2-way map, sorry. You could wrap the two maps in your own class that exposes the functionality of a 2-way map.

Nested Map Structure Create Empty Map

I'm don't get this to work. I want to initialize the following structure for later use.
map<pair<string,string>, map<string,vector<fs::path>>> filelist;
Thats means I provide the string pair for the first map. (e.g., "id1","id2") with an empty second map that I can fill in later. So I want to do something like this:
filelist.insert(
pair<pair<string,string>, pair<string,vector<fs::path>>>
(make_pair("id1","id2), **empty map??**));
Obviously when when I stick to the vector instead of the nested map I can do this:
filelist.insert(pair<pair<string, string>, vector<vector<fs::path>>>
(make_pair("id1","id2"),{}) );
But how can I initialize an empty map? Or is there an alternative data structure? Thanks
I want to initialize the following structure for later use.
map<pair<string,string>, map<string,vector<fs::path>>> filelist;
You can use operator[] to assign it.
Some thing like follows.
filelist[ std::make_pair("string1", "string2") ] = value();
where, using value = std::map< std::string, std::vector<int>>;
See live action: https://www.ideone.com/lxQir7
is there an alternative data structure?
A suggestion to reduce the complexity of your chosen data-structure is possible, only when you explain your problem and data manipulation requirements more in detail.
#include <iostream>
#include <map>
#include <string>
#include <vector>
using key = std::pair< std::string, std::string>;
using value = std::map< std::string, std::vector<int>>;
int main ()
{
std::map< key , value > filelist;
value vMap;
vMap["string"] = {1,2,3,4};
filelist[ std::make_pair("string1", "string2") ] = vMap;
// to print or access
for(auto& key_itr: filelist)
{
std::cout<< "KEY: " << key_itr.first.first << " " << key_itr.first.second << "\t VALUE: ";
for(auto& val_itr: key_itr.second)
{
std::cout << val_itr.first << " ";
for(const auto& vec: val_itr.second) std::cout << vec << " ";
}
std::cout << std::endl;
}
return 0;
}
You can initialize an empty map, simply by its default constructor.
It is always a better idea to name your newly introduced data types.
After all, it's a relatively complex data structure. Why don't you simplify your problem?
#include <map>
#include <vector>
#include <filesystem>
using namespace std;
using namespace std::filesystem;
using string_to_vector_of_path_map = map<string, vector<path>>;
using pair_of_strings = pair<string, string>;
using my_map = map<pair_of_strings, string_to_vector_of_path_map>;
my_map filelist;
int main()
{
filelist.insert(make_pair(make_pair("id1", "id2"), string_to_vector_of_path_map()));
return 0;
}

Search for key by vector in map

So, we have a school-project in creating a phonebook where you should be able to look up phone numbers by searching for the name. I decided to use a map with a string for the phone number and and a vector of strings for the name, due associated number should be able to have multiple names in it.
However, due to us jumping straight from Python to C++ without any explanation of the syntax or the language, I am having a hard time coming up with a way to look for the number by searching for names.
The class I am using looks like this
class Telefonbok
{
public:
void add(string namn, string nummer)
{
map<string, vector<string>>::iterator it = boken.find(nummer);
if (it != boken.end())
{
cout << "This number already exists, please choose another";
}
else
{
namn_alias.push_back(namn);
boken[nummer] = namn_alias;
}
}
void lookup(string name)
{
for (map<string, vector<string>>::iterator sokning = boken.begin(); sokning != boken.end(); sokning++)
cout << "Hello!";
}
private:
vector<string> namn_alias;
string nummer;
map<string, vector<string>> boken;
};
What I am trying to do in lookup function is to search for a phone number by the names in the vector, but I am stumped on how to proceed with looking through the vector inside the for-loop.
The plan was to go through the Map keys one by one to find the vector that contains the searched-for name. Any tips on how to proceed or some functions I have missed that can be used for this?
Algirdas is correct, you should read up on C++.
Assuming you are mapping name to 1-or-more numbers, but only 1 number per name...
#include <cstddef>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
class Telefonbok
{
public:
void add(string namn, string nummer) {
auto it = nummer_namn.find(nummer);
if (it != nummer_namn.end()) {
cout << "This number already exists, please choose another" << endl;
}
else {
nummer_namn[nummer] = namn;
namn_nummer[namn].push_back(nummer);
}
}
void lookup(string name) {
auto it = namn_nummer.find(name);
if (it == namn_nummer.end()) {
cout << "Unable to find any numbers for " << name << ", sorry." << endl;
return;
}
for (auto const& sokning : it->second)
cout << name << " : " << sokning << endl;
}
private:
map<string, vector<string>> namn_nummer;
map<string, string> nummer_namn;
};
int main() {
Telefonbok bok;
bok.add("Eljay", "789");
bok.add("Eljay", "456");
bok.add("Beaker", "123");
bok.lookup("Eljay");
bok.lookup("Beaker");
bok.lookup("Bunsen Honeydew");
return EXIT_SUCCESS;
}

C++ map::erase() does not erase data

I'm trying to test C++ map::erase() with the following code:
//file user.h
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
class User {
string name;
int id;
public:
User(const string& name, int id) : name(name), id(id) {}
int getID() const {return id;}
~User(){}
};
//file main.cpp
#include "user.h"
using namespace std;
typedef map<string, User*> Dict;
int main()
{
Dict dict;
dict["Smith"] = new User("Smith", 666); //Id = 666
dict["Adams"] = new User("Adams", 314); //Id = 314
auto it = dict.find("Adams"); //look for user 'Adams'
if (it == dict.end())
//show 'not Found' if didn't find 'Adams'
cout << "not Found" << endl;
else
//else, show the Id = 314
cout << "id1: " << it->second->getID() << endl;
//Here I think there is a problem
//I ask to delete Adams from the list
dict.erase(it);
//So in this print the ID shouldn't be found
cout << "id2: " << it->second->getID() << endl;
return 0;
}
After I try to delete the item from the list it seems like it is not deleted as the program shows the following:
pc#pc:~/Test$ ./main
id1: 314
id2: 314
As I understand id2 shouldn't show any value. Is this good or did I misunderstood the use of erase. If yes, how can I delete the item after it is shown?
you are in undefined behavior land. You are using an iterator (it) after you have modified the map. Anything can happen - including apparently working (a bit). You shoud redo
auto it = dict.find("Adams"); //look for user 'Adams'
this will not find anything
Basically you have undefined behavior calling
dict.erase(it);
//So in this print the ID shouldn't be found
cout << "id2: " << it->second->getID() << endl;
The iterator variable isn't somehow reset when it was used with dict.erase(it);.
Also you should take care to call delete before using erase(). Otherwise you would leak memory.
You're erasing a pointer from the map, but the object being pointed to by the map isn't being erased. You need to spend some time learning about memory management in c++.