For Windows 7 64-bit
So I set up my map
map<string, string> database;
database["user"] = "123";
It's part of the main function, but how can I print the contents of the map? And most importantly how can it be turned into a global variable so I can use it by other functions? I'm trying to put my printing map process in a different function than where the map was made.
You can iterate through map with following code:
for(auto it = database.begin(); it != database.end(); ++it) {
// it->first is your key
// it->second is value of particular key
std::cout << "Key: " << it->first << std::endl;
std::cout << "Value: " << it->second << std::endl;
// value can be reached as follows as well
std::cout << "Value: " << database[it->first] << std::endl;
}
Try this:
#include <map>
#include <iostream>
// this will be a global variable
map<string, string> database;
int main()
{
database["user"] = "123";
map<string, string>::iterator it;
for(it = database.begin(); it!= database.end(); it++)
{
pair<string, string> p = *it;
cout << "key=\"" << p.first << "\" value=\"" << p.second << "\"" << endl;
}
}
I'd advise to use C++11 features, like for_each, range based begin and end and lambda:
std::for_each(std::begin(database), std::end(database),
[&database](std::pair<std::string, std::string> p) {
std::cout << "Key : " << p.first << "\tValue : " << p.second << std::endl;
});
Second I'd avoid to make your map a global variable. There are reasons for global definitions, but for a container? (This maybe depends how big your project is, but I'd say let's get rid of any global variable, and if it is really needed put it inside a namespace)
Why not encapsulate your map into a class and provide functions to alter the content of your map, even your printing facility? If you want to make sure, that every user of your class shall have the same content of the map (translated this means the same instance), you can turn your class into a singleton.
If you have C++1 available (and as a sidenote to Stefan's answer: You don't actually need std::for_each or lambdas in C++11), iteration can be done using range based for:
for ( std::pair<std::string,std::string> const &p : database)
std::cout << "key=\"" << p.first << "\" value=\"" << p.second << "\"" << std::endl;
If you want to use it in other functions, think about several things instead of making the variable global:
Aggreate/incapsulate all functions using the database in a class
Pass the map object to your functions
Related
I have a fairly easy problem: I have an std::map<int,T> and another std::set<int> (can be std::vector or similar too).
In the map I store items, and in the other container I'm storing favorites (of the map).
At some point, I'd need to retrieve (all) items from the map, but starting with the favorites defined by the other container.
Here is my minimal repro, I solved it very ugly, and ineffective:
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
map<int, string> myMap;
set<int> myFavorites;
int main()
{
myMap.emplace(1, "but I don't like this");
myMap.emplace(12, "So it will go below");
myMap.emplace(31, "This one will come first, and");
myMap.emplace(44, "under my favorites");
myMap.emplace(52, "then this will follow");
myFavorites.insert(52);
myFavorites.insert(31);
cout << "My map:" << endl;
for(auto p : myMap) {
cout << "#" << p.first << "=" << p.second << endl;
}
cout << endl << "My favorites:" << endl;
for(auto p : myFavorites) {
cout << "#" << p << endl;
}
cout << endl << "All items starting with my favorites:" << endl;
for(auto p : myFavorites) {
auto item = myMap.find(p);
if (item != myMap.end()) cout << "#" << item->first << "=" << item->second << endl;
}
for(auto p : myMap) {
if (myFavorites.find(p.first) != myFavorites.end()) continue;
cout << "#" << p.first << "=" << p.second << endl;
}
}
What really bothers me is the last loop, where each iterations would call find on the set.
Required output is:
All items starting with my favorites:
#31=This one will come first, and
#52=then this will follow
#1=but I don't like this
#12=So it will go below
#44=under my favorites
Here is the above source in Coliru for making it easier: https://coliru.stacked-crooked.com/a/731fa76d90bfab00
Both map and set might be changed, but replacements needs to implement the same interfaces as originals.
I'm looking for a way to solve this more efficient than my original "brute-force" one.
Please note: map must not be "reordered"! I just need to query (retrieve) its items with custom sorting!
Note2: I know map can have a comparison operator. But I'd need to have the original order usually, and sometimes I'd need to have the custom sort!
Note3: Boost is not available and compiler is C++14 capable.
Both std::map and std::set use the same strict weak ordering for ordering its contents.
You can take advantage of this. You know that if you iterate over the map you will get the keys in the same order as they are in the set, therefore all it takes is a little bit of clever logic, something like:
auto map_iter=myMap.begin();
for(auto p : myFavorites) {
while (map_iter != myMap.end())
{
if (map_iter->first == p)
cout << "#" << map_iter->first << "=" << map_iter->second << endl;
if (map_iter->first > p)
break;
++map_iter;
}
}
It may still make sense to use find() in some edge cases, specifically when myFavorites is significantly smaller than myMap, in which case a few calls to find() might be faster than iterating over (most of) the entire map.
I'm a boost newbie.
How do I access the object from an iterator? I have something like:
boost::container::vector<std::string>::iterator plitr = myvec.begin();
while (plitr != myvec.end()){
std::cout << "data at index[" << plitr - myvec.begin() << "]: " << plitr->x <<std::endl;
plitr++;
}
But I realize that plitr->x does not exist nor am I sure if the index can be calculated the way I think.
Can anyone help?
The usage of boost::vector is identical to std::vector. Calculating the index hence works the way you showed, because the iterator fulfills random access criteria. Concerning access to the object, you want to dereference the iterator. Change your loop to
while (plitr != myvec.end()){
std::cout << "data at index[" << plitr - myvec.begin() << "]: " << *plitr <<std::endl;
plitr++;
}
and it will work (note the *plitr, that't the dereferencing part). Just as a side note, using a range based for loop to access every std::string in myvec might be more convenient here:
for (auto&& str : myvec)
std::cout << str << std::endl;
I am trying to print a map in an organized way. My map is defined like this:
map<std::string,std::vector<message *> > data;
where message is a struct like this:
struct message{
static unsigned int last_id;
unsigned int id;
std::string msg;
std::string timestamp;
message(const std::string& recvbuf_msg,const std::string& a_timestamp) :
msg(recvbuf_msg), timestamp(a_timestamp), id(++last_id)
{
}
};
I tried this way of printing it:
std::cout << (data[username]).at(0)->msg << std::endl;
But it gives a debug error when reaching that function, how can i solve it?
Error R6010 - abort() has been called suggests that either there is no entry for key username in the map, or the vector of messages for that user is empty. You need to make sure the containers are nonempty before accessing elements. It is a good idea to use iterators, here is an example of how to print the messages for all usernames:
for(auto mapIt = data.cbegin(); mapIt != data.cend(); ++mapIt)
{
std::cout << "printing data for " << mapIt->first << ":" << std::endl;
for(auto vectIter = mapIt->second.cbegin(); vectIter != mapIt->second.cend(); ++vectIter)
{
std::cout << (*vectIter)->msg << ", " << (*vectIter)->timestamp << ", "
<< (*vectIter)->id << std::endl;
}
}
The code uses auto, so if you are not using a C++11 compliant compiler, you will have to write the iterator types yourself.
I want to print a pair, e.g.
std::cout << make_pair(std::string,int) << endl;
But it doesn't compile because operator<<(ostream &, std::pair<std::string,int>) is not defined.
But because we now have c++11 with lambda functions, I can use lambda functions with a for_each expression to work on containers.
For the above case how could I supply an "in-place method" which can be used by ostream to print the pair?
Pairs (and other tuples) aren't really like containers, because their elements have heterogeneous types. They can't be iterated over in the normal way. So a lambda isn't really applicable here.
If you want, just define an output_pair template function which takes an ostream and a pair, and outputs the two elements of the pair. Or if you wanted to keep the extraction style, you could have output_pair return an output_pair_struct which does nothing but hold a copy of the tuple, and define an operator<< on the output_pair_struct which did the actual work, so that you could have std::cout << output_pair(mypair) << endl;.
For the above case how could I supply an "in-place method" which can be used by ostream to print the pair?
auto print = [&](const std::pair<std::string,int>& p) {
std::cout << p.first << ", " << p.second << "\n";
};
std::map<std::string,int> sequence = { /* ... */ };
for_each(sequence.begin(), sequence.end(), print);
for(const auto& p: sequence)
print(p);
or:
for_each(sequence.begin(), sequence.end(),
[&](const std::pair<std::string,int>& p) {
std::cout << p.first << ", " << p.second << "\n";
});
Either way, this is too complicated. You should write it like this:
for(const auto& p: sequence)
std::cout << p.first << ", " << p.second << "\n";
I'm trying to write a copy constructor for an object managing a STL map containing pointers, where the key is a string. However, when I attempt to insert new values in the map, the pointers are set to NULL:
// ...
for(std::map<std::string, data_base*, order>::const_iterator it = other.elements.begin();
it != other.elements.end(); ++it){
data_base *t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
elements[it->first] = t;
std::cout << "INSERTED: " << std::hex << elements[it->first] << std::endl;
}
// ...
other is the object being copied and elements the map. The clone() method returns a pointer to a new object (via new).
Running the code above I get something like:
CLONE: 0xcfbbc0
INSERTED: 0
I'm not a very experienced programmer and this issue is probably simple to fix, but I didnt find any solution to it searching around.
Thanks a lot for your time.
I don't see any problem with this code, other than maybe
std::map<std::string, data_base*, order>::const_iterator it
Here order gives the key comparator to use to sort the pairs contained in the map (often implemented as a tree).
Maybe you're doing something wrong in it, making your [] operator don't find the right ke, making your last line logging a new pair with a null ptr.
First, try without that order, using the default key-comparator (std::less), then if it don't work, post your order definition and the map declaration. If it's not enough, just provide a simple complete program that reproduce the problem.
I just wrote a simple similar test, using the default key-comparator :
#include <map>
#include <string>
#include <iostream>
struct Data
{
int k;
Data* clone() { return new Data(); }
};
typedef std::map< std::string, Data* > DataMap;
DataMap data_map;
int main()
{
data_map[ "hello" ] = new Data();
data_map[ "world" ] = new Data();
DataMap other_map;
for( DataMap::const_iterator it = data_map.begin(); it != data_map.end(); ++it)
{
Data*t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
other_map[it->first] = t;
std::cout << "INSERTED: " << std::hex << other_map[it->first] << std::endl;
}
std::cin.ignore();
return 0;
}
On VS2010SP1, this outputs :
CLONE: 00034DD0
INSERTED: 00034DD0
CLONE: 00035098
INSERTED: 00035098
So it should be the problem, or maybe you're doing something wrong before.
Try this out, to help debug the issue. I'd recommend double-checking that the order function is correct. You can remove it to use std::less<T>, which is known to work.
// ...
typedef std::map<std::string, data_base*, order> string_db_map;
for(string_db_map::const_iterator it = other.elements.begin();
it != other.elements.end();
++it)
{
data_base *t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
std::pair<string_db_map::iterator, bool) result = elements.insert(
string_db_map::value_type( it->first, t));
if ( !result.second )
{
std::cout << "element['" << it->first << "'] was already present, and replaced." << std::endl;
}
std::coud << "INSERTED [iterator]: " << std::hex << (*result.first).second << std::endl;
std::cout << "INSERTED [indexed]: " << std::hex << elements[it->first] << std::endl;
}
// ...