I am trying to copy a subset of a std::map structure to a new map structure in the most efficient way. I can only think to a plain vanilla solution like this:
// Example program
#include <iostream>
#include <string>
#include <map>
int main()
{
// build map
std::map<int, int> mymap;
size_t num_el = 10;
for(size_t i = 0; i < num_el; ++i)
{
mymap.insert(std::pair<int,int>(i,i));
}
// copy submap
int start_index = 5;
std::map<int,int> output_map;
std::map<int,int>::const_iterator it;
for(it = mymap.find(start_index); it != mymap.end(); ++it)
{
output_map.insert(*it);
}
//print result
std::map<int,int>::const_iterator pit;
for(pit = output_map.begin(); pit != output_map.end(); ++pit)
{
std::cout << pit->second << " , ";
}
std::cout << std::endl;
}
Is there a better way to do this?
The insert method allows you to specify a range like so:
auto range_start = mymap.find(5);
auto range_end = mymap.end();
output_map.insert(range_start, range_end);
If you want additional flexibility on choice of the copied elements, copy_if will provide it:
std::copy_if(
begin(mymap),
end(mymap),
std::inserter(output_map, begin(output_map)),
[&start_index](std::pair<int,int> p) { return p.first >= start_index; }
);
It's a bit tricky to use it for std::map (and also e.g. std::set), but std::inserter works nicely.
Of course the simpler std::copy could be used similarly to the range constructor.
You can use map range constructor (c++11):
std::map<int,int> output_map{mymap.find(start_index), mymap.end()};
and if you need older standard:
std::map<int,int> output_map( mymap.find(start_index), mymap.end() );
And insert works too
Related
Hey my mentor(kinda) gave me this task and i am having trouble solving it .
So basically i am getting const vector<string> &data full of string and i need to check their place, like where are they in that vector so here is an example:
getting an input with: data={"chair","desk","table","chair","chair","desk"}
my output should be:{"chair" ->{0,3,4},"desk"->{1,5} , "table"->{2}}
so what i did is :
map<string, set<size_t>> index(const vector<string> & data) noexcept {
map<string, set<size_t>> res; // thats gon be my return
if (data.empty()){
return res;
}
for (auto it = data.begin(); it != data.end(); it++) {
if(res.find(*it)==res.end()){
//so basically when we are the first , and nothing is there so
// it should insert for example =chair , and a 0
res.insert(std::make_pair(*it, 0)); // i tried it like that it doesnt work
}else{
// when the string "chair is already in there i want to append to the set the current index where we are and thats what i dont know how
}
}
return res;
}
how to get the index of the current string and append it to my set<size_t> so that is works as mentioned?
You can implement this quite easily by using an index-based loop:
map<string, set<size_t>> index(const vector<string> & data)
{
map<string, set<size_t>> res;
for (std::size_t i = 0u; i < data.size(); ++i)
res[ data[i] ].insert(i);
// ^string^ ^index
return res;
}
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
std::map<std::string, std::set<size_t>> index(const std::vector<std::string>& data) noexcept
{
std::map<std::string, std::set<size_t>> res;
for (size_t idx = 0u; idx < data.size(); idx++)
{
res[data[idx]].insert(idx);
}
return res;
}
int main()
{
const std::vector<std::string> data={"chair","desk","table","chair","chair","desk"};
auto map = index(data);
for( auto it = map.begin(); it != map.end(); ++it )
{
std::cout << (*it).first << " ->";
for( auto it_id = (*it).second.begin(); it_id != (*it).second.end(); it_id++)
{
std::cout << " " << *it_id;
}
std::cout << "\n";
}
}
So your problem can be solved with a oneline as seen in the index function - but why ?
There is a reason youre getting all these different std container types as input - each of them have some guarantees as can be looked up in the documentation.
vector elements are continous in memory (sequence container).
while map and set are associative and have unique keys.
Consult the documentation for container properties (i linked to the vector - but the rest are also present in the tree to the left).
http://www.cplusplus.com/reference/vector/vector/
The lesson is knowing which tool(container) to use to solve specific tasks
And once you have that nailed .. making sure items are unique or how to sequence every odd element in a vector should be a breeze.
Best way to store mapping from strings to ints, and the other way around at the same time?
For example, I have a vector called columnNames, and I would like to
be able to get a column name given its index, that is its position, but then
also given its position I would like to get its name. One way to do this
is to use a map and store column names in order, and this way
I will be able to do what I want, but I wonder if there is a better way?
Depends what you mean by "better". Boost.MultiIndex is a very flexible way do this and other things like it, while Boost.Bimap is more specialized.
For this specific case, you could use a map<string,int> together with a vector<const string*> for the lookup from index to string (because indexes by nature form a contiguous sequence). The pointers in the vector point to keys in the map. This is easiest if you don't modify the columns once they're set up, but even if you do it's possible to update the vector to match.
Not aware of something in C++11 but this might work:
http://www.boost.org/doc/libs/1_46_1/libs/bimap/doc/html/index.html
Boost.Bimap is a bidirectional maps library for C++. With Boost.Bimap
you can create associative containers in which both types can be used
as key. A bimap can be thought of as a combination of a
std::map and a std::map. The learning curve of bimap is
almost flat if you know how to use standard containers. A great deal
of effort has been put into mapping the naming scheme of the STL in
Boost.Bimap. The library is designed to match the common STL
containers.
A (probably worse, O(n) ) solution with lambdas:
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
map<string, int> m = {{"hello", 1}, {"world", 2}};
int main() {
int findVal = 2;
auto it = find_if(m.begin(), m.end(), [findVal](const pair<string,int> & p) {
return p.second == findVal;
});
if(it != m.end())
cout << it->second << "->" << it->first << "\n";
string findStr = "hello";
auto itStr = find_if(m.begin(), m.end(), [findStr](const pair<string,int> & p) {
return p.first.compare(findStr) == 0;
});
if(itStr != m.end())
cout << itStr->second << "->" << itStr->first << "\n";
return 0;
}
If you're searching for something horribly simple (and probably terribly inefficient too), use this:
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
map<string, int> m = {{"hello", 1}, {"world", 2}};
class notFound
{ };
string findFromInt(const int& val)
{
map<string,int>::iterator it = m.begin();
while(it != m.end())
{
if(it->second == val)
return it->first;
it++;
}
throw new notFound();
}
int findFromString(const string& str)
{
map<string,int>::iterator it = m.begin();
while(it != m.end())
{
if(it->first.compare(str) == 0)
return it->second;
it++;
}
throw new notFound();
}
int main() {
int findVal = 2;
try {
string str = findFromInt(findVal);
cout << "found: " << str;
} catch(notFound *obj) {
cout << "Value not found";
}
string findStr = "hgggllo";
try {
int val = findFromString(findStr);
cout << "found: " << val;
} catch(notFound *obj) {
cout << "Value not found";
}
return 0;
}
http://ideone.com/2FP64h
In your usecase, I would prefer the method you mention (map + vector) over bi-maps.
Your usecase is special in a way that your ints start at zero and are consecutive.
Your proposed solution is simple, highly efficient (especially hashmap, aka. unordered_map + vector) and does not require any outside library.
You may use a biderectional map, like this one:
boost bimap
I'm new to stl in c++ and I want to iterate over a vector of maps. Then, if one of these maps satisfies a certain condition, I want to make a copy of this map an then insert the copy into the vector.
For example:
int main(){
vector<map<string, int> > my_vector;
map<string, int> my_map;
my_map["zero"] = 0;
my_vector.push_back(my_map);
for(vector<map<string, int> >::iterator iter1 = my_vector.begin();
iter1 != my_vector,end();
iter1++){
for(map<string, int>::iterator iter2 = (*iter1).begin();
iter2 != (*iter1).end();
iter2++){
if(iter2->second == 0){
// make a copy of the map (*iter1), make some changes on it,
// then insert the copy in the vector
}
}
}
return 0;
}
I have tried with:
map<string, int> new_map = *iter1;
new_map["zero"]++; // to avoid an infinite loop
my_vector.push_back(new_map);
But the program crashes. No compiler error, only a program crash.
Then I found this question and answers in stackoverflow, so I tried another way like this:
int main(){
vector<map<string, int> > my_vector;
map<string, int> my_map;
my_map["zero"] = 0;
my_vector.push_back(my_map);
int remaining = my_vector.size();
int current_position = 0;
while(remaining>0){
for(map<string, int>:: iterator iter1 = my_vector[index].begin();
iter1 != my_vector[index].end();
iter1++){
if(iter1->second == 0){
map<string, int> new_map = my_vector[0];
new_map["zero"]++; // to avoid an infinite loop
my_vector.push_back(new_map);
}
}
index++;
remaining = my_vector.size()-index;
}
return 0;
}
But the program is still crashing, so I think the problem (or one of the problems) would be not only the iterator, but also the "copy" of the map.
If anyone have an idea of how I should do this, I will really appreciate it.
Your program may crash because push_back can invalidate iterators. For example if a call to push_back leads to memory reallocation (it happens when you exceed current capacity of the vector) then all vector's iterators become invalid (they point to deallocated memory).
To solve that problem you can use indexes instead of iterators to access the vector's elements or you can push_back new elements to another copy of the vector.
In other words: don't use an old vector::iterator after you push_back'ed a new element into the vector.
One way you can do is:
size_t orig_size = my_vector.size();
for( size_t i = 0; i < orig_size; i++ ) {
//...
}
Other way:
Build a new vector and then afterwards append the contents of this new vector to the original vector.
I probably have the wrong idea, but I think you can do something like this. You iterate over the elements of the vector, use the predicate to see if a current element fits the criteria, then use back_inserter.
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
vector<map<string, int> > my_vector;
map<string, int> my_map;
my_map["zero"] = 0;
my_vector.push_back(my_map);
auto predicate = [](const map<string, int>& cur) {
for (const auto& pair : cur)
{
if (pair.second == 0)
return true;
}
return false;
};
std::copy_if(my_vector.begin(), my_vector.end(), back_inserter(my_vector), predicate);
return 0;
}
This is my solution. It uses C++11 (because it looks so much better). Especially with collections C++11 gives the workflow a real boost.
#include <vector>
#include <string>
#include <map>
#include <iostream>
using namespace std ;
void merge(vector<map<string,int>>& from, vector<map<string,int>>& to)
{
for(auto entry : from) {
to.push_back(entry) ;
}
}
void search(vector<map<string,int>>& vec)
{
int iCount = 0 ;
vector<map<string, int>> cVector ;
for(auto vmap : vec) {
for(auto mapentry : vmap) {
if(mapentry.second == 0) {
iCount++ ;
map<string, int> new_map = vmap ;
cVector.push_back(new_map) ;
}
}
}
cout << "iCount: " << iCount << endl ;
merge(cVector, vec) ;
}
int main(){
vector<map<string, int> > my_vector;
map<string, int> my_map;
my_map["zero"] = 0;
my_map["third"] = 2 ;
my_map["second"] = 0 ;
my_vector.push_back(my_map);
cout << my_vector.size() << endl ;
vector<map<string,int>> cVector ;
search(my_vector) ;
cout << my_vector.size() << endl ;
return 0;
}
As written by Alex Antonov the push_back invalidates your Iterator because the memory gets reallocated. My answer creates a new vector and copies the entries after the search back in the original vector (my_vector).
About range base for-loops see this.
I have a map and I want the first column i.e (*it).first to be pushed back into a vector then (*it)->second to be pushed back into another vector
Is this the best way to do it?
std::vector<std::string>test;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
}
My other question is if i have a loop i.e
how would I insert all the integers i into (*it).first?
for(int i = 0; i < 10; i++)
{
// 1 - 10 will go in (*it).first
}
I want to have some integers in (*it).first and have associated values in (*it).second;
Use std::transform.
First define two functions key and value which take the pair of strings and return the first or second value, respectively.
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
const std::string& key(const std::pair<std::string, std::string>& keyValue)
{
return keyValue.first;
}
const std::string& value(const std::pair<std::string, std::string>& keyValue)
{
return keyValue.second;
}
Then use std::transform from <algorithm> with the functions to transform the map into either a vector of keys or a vector of values.
int main()
{
using namespace std; // be explicit normally, trying to be brief here
map<string, string> contacts;
contacts["alice"] = "555-2701";
contacts["bob"] = "555-2702";
vector<string> keys(contacts.size());
vector<string> values(contacts.size());
transform(contacts.begin(), contacts.end(), keys.begin(), key);
transform(contacts.begin(), contacts.end(), values.begin(), value);
cout << "Keys:\n";
copy(keys.begin(), keys.end(), ostream_iterator<string>(cout, "\n"));
cout << "\n";
cout << "Values:\n";
copy(values.begin(), values.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
Output:
Keys:
alice
bob
Values:
555-2701
555-2702
Your first question, "how can I push the first column of my map into one vector and the 2nd column into another" is solved thus:
std::map<std::string, std::string> mymap;
std::vector<std::string> keys;
std::vector<std::string> values;
for ( std::map<std::string,std::string>::iterator it=mymap.begin() ; it != mymap.end(); ++it )
{
keys.push_back(it->first);
values.push_back(it->second);
}
Your second question, "how would insert all the integers i into (*it).first ?" is solved thus:
std::map<int, int> mymap2;
for(int i = 0; i < 10; i++)
{
// Insert default value into map
// This sets '(*it).first' to 'i' and
// '(*it).second' to a default value (in
// this case, 0).
mymap2[i];
}
or
std::map<int, int> mymap3;
for(int i = 0; i < 10; i++)
{
// Insert specified value into map
// this sets '(*it).first' to 'i', and
// '(*it).second' to the value returned from the function.
maymap3[i] = ChooseSpecificValue(i);
}
Well, it can be done with a simple loop:
for (auto const& p: mymap) {
vec1.push_back(p.first);
vec2.push_back(p.second);
}
Or using the std::transform algorithm, though it's quite verbose here:
std::transform(mymap.begin(), mymap.end(), std::back_inserter(vec1),
[](MyMap::const_reference p) { return p.first; });
Assuming you've declared your map as string key and value (ie map<string, string> mymap; then it would be like below, also assuming you've declare 'it' variable as map<string, string>::iterator it, etc:
std::vector<std::string> test;
std::vector<std::string> second;
std::map<string, string>::iterator it;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
second.push_back((*it).second);
}
Not sure about your next question.
The first part of your question:
std::vector<std::string> test;
std::vector<std::string> test2; // assuming map is from string to string
for (it = mymap.begin(); it != mymap.end(); ++it)
{
test.push_back(it->first); // push first in one vector
test2.push_back(it->second); // push second in another vector
}
So, yes a simple for can do what you want.
The second part of your question:
Since you are updating the key of the map, you would need to remove it from the map and insert the changed one. So:
std::string first, second;
first = it->first;
second = it->second;
mymap.erase(it); // be careful with invalidating iterator
// change first
mymap[first] = second;
To change first by adding all integers i to it, that would really depend on the type of first. For example with a string, you may mean something like this:
ostringstream sout;
for (int i = 0; i < 10; ++i)
sout << (i?" ":"") << i;
first = sout.str();
Or if first is for example a set, you may mean something like this:
for (int i = 0; i < 10; ++i)
first.insert(i);
and my other question is if i have a loop i.e how would insert all the
integers i into (*it).first?
In the case of a std::map, you can't modify the iterator returned like that ... the key member (i.e., the first) in the std::map key/value pair data-structure is intentionally designated as a constant value, and is initialized to its constant value at the beginning of the key/value pair's lifetime in the std::map data-structure. If the keys weren't constant, you would end up creating havoc when you change the key, since the nodes in a std::map are suppose to be sorted by the keys. The second member of the key/value pair data-structure is the member that can be changed.
So if you want to insert a set of key/value pairs in a map, you could simply do the following:
std::map<int, int> mymap;
int some_other_value = 100;
for (int i=0; i < 10; i++)
{
mymap[i] = some_other_value++;
}
it here will be an iterator which will point to one of the position in map and at max have one first and second value for one iterator . At max you can have multiple key or same key holding same/different values depending on key/value combination.
As far as pushing the value in the vector for a key in map is concern you can do it in the same way you are pushing the key
std::vector<std::string>test;
std::vector<std::string>test2;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
test2.push_back((*it).second);
}
Neways yours question is very unclear .
Just in case you want to deal with different data types in your map I would template a generic copy function:
template <class A, class B>
void mycopy(std::map<A, B>&m, std::list<A>& keys, std::list<B>& values) {
typename std::map<A, B>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
keys.push_back( (*it).first );
values.push_back( (*it).second );
}
}
Mixing it up:
std::map<int, std::string> mymap;
std::list<int> keys;
std::list<std::string> values;
mymap[1] = "string1";
mymap[2] = "string2";
mycopy(mymap, keys, values);
std::map<std::string, int> mymap1;
std::list<std::string> keys1;
std::list<int> values1;
mymap1["string1"] = 1;
mymap1["string2"] = 2;
mycopy(mymap1, keys1, values1);
Edit: yes __copy isnt the best definition. Thanks
I want to remove some elements from a vector and am using remove_if algorithm to do this. But I want to keep track of the removed elements so that I can perform some operation on them later. I tried this with the following code:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct IsEven
{
bool operator()(int n)
{
if(n % 2 == 0)
{
evens.push_back(n);
return true;
}
return false;
}
vector<int> evens;
};
int main(int argc, char **argv)
{
vector<int> v;
for(int i = 0; i < 10; ++i)
{
v.push_back(i);
}
IsEven f;
vector<int>::iterator newEnd = remove_if(v.begin(), v.end(), f);
for(vector<int>::iterator it = f.evens.begin(); it != f.evens.end(); ++it)
{
cout<<*it<<"\n";
}
v.erase(newEnd, v.end());
return 0;
}
But this doesn't work as remove_if accepts the copy of my functor object, so the the stored evens vector is not accessible. What is the correct way of achieving this?
P.S. : The example, with even and odds is just for example sake, my real code is somethinf different. So don't suggest a way to identify even or odds differently.
The solution is not remove_if, but it's cousin partial_sort partition. The difference is that remove_if only guarantees that [begin, middle) contains the matching elements, but partition also guarantees that [middle, end) contains the elements which didn't match the predicate.
So, your example becomes just (note that evens is no longer needed):
vector<int>::iterator newEnd = partition(v.begin(), v.end(), f);
for(vector<int>::iterator it = newEnd; it != v.end(); ++it)
{
cout<<*it<<"\n";
}
v.erase(newEnd, v.end());
Your best bet is std::partition() which will rearrange all elts in the sequence such as all elts for which your predicate return true will precede those for which it returns false.
Exemple:
vector<int>::iterator bound = partition (v.begin(), v.end(), IsEven);
std::cout << "Even numbers:" << std::endl;
for (vector<int>::iterator it = v.begin(); it != bound; ++it)
std::cout << *it << " ";
std::cout << "Odd numbers:" << std::endl;
for (vector<int>::iterator it = bound; it != v.end(); ++it)
std::cout << *it << " ";
You can avoid copying your functor (i.e. pass by value) if you pass ist by reference like this:
vector<int>::iterator newEnd = remove_if(v.begin(), v.end(),
boost::bind<int>(boost::ref(f), _1));
If you can't use boost, the same is possible with std::ref. I tested the code above and it works as expected.
An additional level of indirection. Declare the vector locally, and
have IsEven contain a copy to it. It's also possible for IsEven to
own the vector, provided that it is dynamically allocated and managed by
a shared_ptr. In practice, I've generally found the local variable
plus pointer solution more convenient. Something like:
class IsEven
{
std::vector<int>* myEliminated;
public:
IsEven( std::vector<int>* eliminated = NULL )
: myEliminated( eliminated )
{
}
bool
operator()( int n ) const
{
bool results = n % 2 == 0;
if ( results && myEliminated != NULL ) {
myEliminated->push_back( n );
}
return results;
}
}
Note that this also allows the operator()() function to be const. I
think this is formally required (although I'm not sure).
The problem that I see with the code is that the evens vector that you create inside the struct gets created everytime the remove_if algorithm calls it. So no matter if you pass in a functor to remove_if it will create a new vector each time. So once the last element is removed and when the function call ends and comes out of the function the f.evens will always fetch an empty vector. This could be sorted in two ways,
Replace the struct with a class and declare evens as static (if that is what you wanted)
Or you could make evens global. I wouldn't personally recommend that( it makes the code bad, say no to globals unless you really need them).
Edit:
As suggested by nabulke you could also std::ref something likke this, std::ref(f). This prevents you from making the vector global and avoids for unnecessary statics.
A sample of making it global is as follows,
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector<int> evens;
struct IsEven
{
bool operator()(int n)
{
if(n % 2 == 0)
{
evens.push_back(n);
return true;
}
return false;
}
};
int main(int argc, char **argv)
{
vector<int> v;
for(int i = 0; i < 10; ++i)
{
v.push_back(i);
}
IsEven f;
vector<int>::iterator newEnd = remove_if(v.begin(), v.end(), f);
for(vector<int>::iterator it = evens.begin(); it != evens.end(); ++it)
{
cout<<*it<<"\n";
}
v.erase(newEnd, v.end());
return 0;
}
This code seems to work just fine for me. Let me know if this is not what you wanted.
You may have another solution; only if you don't need to remove elts in the same time (do you?). With std::for_each() which returns a copy of your functor. Exemple:
IsEven result = std::for_each(v.begin(), v.end(), IsEven());
// Display the even numbers.
std::copy(result.evens.begin(), result.evens.end(), std::ostream_iterator<int> (cout, "\n"));
Take note that it is always better to create unnamed variables in c++ when possible. Here that solution does not exactly answer your primary issue (removing elts from the source container), but it reminds everyone that std::for_each() returns a copy of your functor. :-)