I'm having trouble printing a multi-dimensional map. I initialize it by
map<string, map<string, vector<double>>> mapData;
void Graph::addToGraph(string start, string next, double cos, double mile)
{
//see if the map contains the starting city string, add if not found
if (mapData.find(start) == mapData.end())
{
mapData[start][start].push_back(invalid);
mapData[start][start].push_back(invalid);
}
//see if the map contains the next city string, add if not found
if (mapData.find(next) == mapData.end())
{
mapData[next][next].push_back(invalid);
mapData[next][next].push_back(invalid);
}
mapData[start][next].push_back(cos);
mapData[start][next].push_back(mile);
double check = mapData[start][next][0];//test case
}
As far as I can tell it seems to me the map has been created correctly. I used a third dimension so I can store the mileage and the price. This will be used in a shortest path algorithm. I want to print each city, as well as all the connecting flights to the city with their mileage and price example, SFA SLC $700 59miles. Is this even possible, and if not how would I change it?
To give a better idea of why I'm using a map, the requirements of the project is that I read in from a file all the information, create a graph from that file, then let the user enter in the abbreviation of the city they want to start. Maps allowed me to specify a spot in an array with a string which was useful for this. But I'm not sure it will work with this algorithm.
{
for (auto kv : mapData)
{
for (auto kvv : kv.second)
{
if (kvv.second != invalid)
{
cout << kv.first << "<-->" << kvv.first;
cout << " costs $" << kvv.second << endl;
}
}
cout << distance(mapData.begin(), mapData.find("SLC"));
}
}
This is the function that I used to print my map. Thanks to Lucas for pointing me in the right direction
One of the easiest ways:
for(auto& kv : map)
{
std::cout << kv.first << std::endl;
std::cout << kv.second.first << std::endl;
for(double d : kv.second.second
{
//..
}
//..
}
If you're doing other things with the data while you're looping over it you can create a vector of keys and push_back() kv.first within the loop, and then create another loop that iterates over the keys. I'm not certain that this is the fastest way but it certainly isn't too slow.
std::vector<std::string> keys;
for(auto& kv : map)
{
keys.push_back(kv.first);
}
for(auto s : keys)
{
for(double d : map[s])
{
//..
}
}
Related
I have a map as below :
std::map< std::string ,int> mapobj;
mapobj["one"] = 1;
mapobj["two"] = 2;
mapobj["three"] =3 ;
how to get key when input is value
EX :
input : 1
output : one
Note : In my case value is unique
A one-to-one mapping is actually quite easy, the fastest way to do it is to probably maintain two maps, one for each direction. It becomes more complicated if it's not one-to-one since you'll need to provide a way to get a collection of values or key, rather than a single one. Happily, you only have the one-to-one requirement.
One of the maps is the one you have now, the other will map the values to a given key, soboth would be:
std::map<std::string, int> forwardmapobj;
std::map<int, std::string> reversemapobj;
and these would be maintained within a bidimap class of some sort.
Whenever you insert to, or delete from, your bidimap, you have to perform the equivalent operation on both internal maps.
For example, here's some pseudo-code. It maintains the two maps and ensures that they'e kept in sync for whatever operations you have that change the keys and values:
class biDiMap:
map<string, int> forwardMap
map<int, string> reverseMap
void add(string key, int val):
if exists forwardMap[key]: throw exception 'duplicate key'
if exists reverseMap[val]: throw exception 'duplicate value'
forwardMapObj[key] = val
reverseMapObj[val] = key
void delKey(string key):
if not exists forwardMap[key]: throw exception 'no such key'
delete reverseMap[forwardMap[key]]
delete forwardMap[key]
void delVal(int val):
if not exists reverseMap[val]: throw exception 'no such value'
delete forwardMap[reverseMap[val]]
delete reverseMap[val]
int getValFor(string key): return forwardMap[key]
string getKeyFor(int val): return reverseMap[val]
Obviously, there's plenty of other stuff you could add but that should form the basis. In any case, you've probably got enough work ahead of you turning that into a C++ class :-)
If you don't want to roll your own solution, then Boost has a very good one that you can pretty well use as is. Boost.Bimap provides a fully-templated bi-directional map that you should be able to use with minimal code, such as the following complete program:
#include <iostream>
#include <string>
#include <boost/bimap.hpp>
using std::string;
using std::cout;
using std::exception;
using boost::bimap;
int main()
{
typedef bimap<string, int> SiMap;
typedef SiMap::value_type SiEntry;
SiMap bidi;
bidi.insert(SiEntry("ninety-nine", 99));
int i = 0;
for (string str: {"one", "two" , "three", "four", "five", "six"}) {
bidi.insert(SiEntry(str, ++i));
}
cout << "The number of entries is " << bidi.size() << "\n\n";
for (auto i = 1; i <= 7; i += 3) {
try {
cout << "Text for number " << i << " is " << bidi.right.at(i) << "\n";
} catch (exception &e) {
cout << "Got exception looking up number " << i << ": " << e.what() << "\n";
}
}
cout << "\n";
for (auto str: {"five", "ninety-nine", "zero"}) {
try {
cout << "Number for text '" << str << "' is " << bidi.left.at(str) << "\n";
} catch (exception &e) {
cout << "Got exception looking up text '" << str << "': " << e.what() << "\n";
}
}
cout << "\n";
return 0;
}
It creates a bi-directional mapping between the textual form of a number and the integral value, then does a few lookups (in both directions) to show that it works:
The number of entries is 7
Text for number 1 is one
Text for number 4 is four
Got exception looking up number 7: bimap<>: invalid key
Number for text 'five' is 5
Number for text 'ninety-nine' is 99
Got exception looking up text 'zero': bimap<>: invalid key
I do notice that this has the "stdmap" tag, so this may not be appropriate. However Boost has boost::bimap<> which will allow you to do what you want: it allows lookup by either key or value.
how to get key when input is value
First, there is no guarantee that value is unique. I realize that you are saying it is unique. Still, conceptually speaking, this is something to keep in mind when looking at the problem.
Second, std::map is not sorted by value. Hence, the most efficient algorithm to look for a value will be O(N) on an average.
Try boost Bimap. all the things you are trying to do can simply be done by it.
1 --> one
2 --> two
...
one --> 1
two --> 2
...
here is a link where a working example is present.
here
I need to find the range of the first elements of a vector pair. I need this range for a map, which counts the duplicate entries in this vector.
Here is a code snipped and how I managed it. Maybe there is another, better solution?
unordered_map<int, int> frequency;
vector<pair<unsigned int,Point>> Roi_Num_Koord;
vector<int> Roi_first_Element;
int main()
{
// Part1: fill the Vector pair
Roi_Num_Koord.emplace_back(make_pair(0,Point(3.6));
Roi_Num_Koord.emplace_back(make_pair(1,Point(4,8));
Roi_Num_Koord.emplace_back(make_pair(2,Point(8.3));
Roi_Num_Koord.emplace_back(make_pair(3,Point(4,6));
// Part 2: now copy the first element to another vector
for (int i = 0; i < Roi_Num_Koord.size(); i++)
{
Roi_first_Element.emplace_back(Roi_Num_Koord[i].first);
}
// Part 3: now do the duplicate search (Code was taken out of the internet)
for (int i : Roi_first_Element)
{
++frequency[i];
cout << "freque "<<frequency[i] << endl;
}
for (const auto& e : frequency)
{
if (e.second == 5)
{
std::cout << "Roi " << e.first << " encountered " << e.second << " times\n";
}
}
}
So is there a possibility to remove Part 2 and find out the range of the first Element of Roi_Num_Koord?, so that I don't have to copy the first elements of this vector to the other vector (Roi_first_Element)
Yes the second step is completely redundant. You just iterate through the container and whenever you need first element of the pair you say it explicitly pretty much like you do in Step 2.
for(const pair<unsigned int,Point>& element : Roi_Num_Koord)
{
++frequency[element.first];
cout << "freque " << frequency[element.first] << endl;
}
I've been working on a simple database system managment and I've come up with:
std::map< std::string, std::vector < std::map < std::string,
boost::variant <std::string, size_t, double bool> > > tables;
I have a map (tables) of vectors (table) of maps (records) and I've allready wrote a function to read a file to it but I'm not really sure how to access the single attributes.
I can print the whole thing with:
for(auto table: tables)
for(auto record : table.second)
for(auto attribute : record) {
std::cout << j.second;
I tried doing something like:
std::cout << tables["credentials"][2]["username"];
This however does not work; it only prints a blank line.
It's most likely that you are using wrong keys of the maps to access the database.
Update your code to print the contents of your database so you can see the keys also in the maps.
for(auto table: tables)
{
std::cout << "Key: " << table.first << std::endl;
for(auto record : table.second)
{
for(auto attribute : record)
{
std::cout << "Key: " << attribute.first
<< ", Value: " << attribute.second << std::endl;
}
}
}
I need some help when iterating over JSON ptree in boost. Here's the structure.
{"drives": [{"busy": false, "eof": false, "density": 88 }]}
What I want to do is to print the key and value eg. busy = false. I've tried the code below but there is no output.
BOOST_FOREACH(ptree::value_type &v, pt.get_child("drives"))
{
cout << v.first << endl; // does not work
cout << v.second.data() << endl; // does not work
cout << v.second.get<string>("busy"); // works
}
So how do I print the key?
Thanks in advance for any help.
I went through some old code and I found the way
BOOST_FOREACH(ptree::value_type &v, pt.get_child("drives"))
{
for(auto iter = v.second.begin(); iter!= v.second.end(); ++iter)
{
std::cout << iter->first << " : " << iter->second.get_value<std::string>() << std::endl;
}
}
You only need to iterate over "drives" if you have more than one "drives", and in your json example you don't have.
In your code you try to print v.first and v.data() but those two doesn't hold the data you think they hold.
v.first supposed to hold the key name of "KeyName":{"busy": false, "eof": false, "density": 88 }
which doesn't exists because this value is part of an array.
the v.data() (If I'm not mistaken) holds the key:value which is an inner presentation and cannot be printed this way.
I really think you should try using a different framework for JSON.
I have a Playlist class that has a vector with Tracks and each Track has a multimap<long, Note> as datamember.
class Track {
private:
multimap<long, Note> noteList;
}
Using an iterator to acces the tracks is no problem, so this part here is working fine:
vector<Track>::iterator trackIT;
try{
for(noteIT = trackIT->getNoteList().begin(); noteIT != trackIT->getNoteList().end(); noteIT++){
cout << "---" << noteIT->second.getName() << endl;
}
}catch (int e){
cout << "exception #" << e << endl;
}
What I want to do next is iterate the Notes of each Track. But starting from this part all output is stopped. So I only get to see the first tracks name. Any cout's after that are not shown and the compiler isn't giving me any errors. Even the cout inside the try catch block isn't working..
vector<Track>::iterator trackIT;
multimap<long, Note>::iterator noteIT;
for(trackIT = this->playlist.getTracklist().begin(); trackIT < this->playlist.getTracklist().end(); trackIT++){
cout << trackIT->getTrackName() << endl;
for(noteIT = trackIT->getNoteList().begin(); noteIT != trackIT->getNoteList().end(); noteIT++){
cout << "---" << noteIT->second.getName() << endl;
}
}
cout << "random cout that is NOT shown" << endl; // this part doesn't show up in console either
Also, the method in my Track class that I'm using to add the Note objects looks like this:
void Track::addNote(Note ¬e) {
long key = 1000009;
this->noteList.insert(make_pair(key, note));
}
// I'm adding the notes to the track like this:
Note note1(440, 100, 8, 1, 1);
note1.setName("note1");
synthTrack.addNote(note1);
Any ideas why the iterator won't work?
Change
noteIT < trackIT->getNoteList().end()
To
noteIT != trackIT->getNoteList().end()
Not all iterators support less than / greater than comparisons.
If you have c++11 you can use a range-based for loop:
for (Note& note : trackIT->getNoteList())
Or you can use BOOST_FOREACH
BOOST_FOREACH (Note& note, trackIT->getNoteList())
You haven't shown the definitions of getTrackList or getNoteList, but there's a common mistake people make - if you return a copy of the container instead of a reference to it, the iterators will be pointing to different containers making comparisons impossible. Not only that but since the containers are temporary any use of the iterators results in undefined behavior.
If you are really hardcoding the track key, then there will only ever be one track in the map because std::map stores unique keys...
long key = 1000009; //If yo are really doing this, this key is already inserted so it will fail to insert more.
Also, if you would like a more elegant approach you could use function object.
struct print_track
{
void operator()(const Track& track)
{
cout << track.getTrackName() << endl;
std::for_each(track.getNoteList().begin(), track.getNoteList().end(), print_track_name());
}
};
struct print_note_name
{
void operator()(const std::pair<long,Note>& note_pair)
{
cout << "---" << note_pair.second.getName() << endl;
}
};
//In use...
std::for_each(playlist.getTracklist().begin(), playlist.getTracklist.end(), print_track());