I have a vector<Person> and want to extract the attribute name of each object, to a vector<string>. The for-each loop does it job correctly, but is there a nicer way to use something like std::copy() and tell him to use a specific method (in this case the name() getter) to copy the values into my vector<string>?
class Person {
int a;
string n;
public:
Person(int age, string name) : a(age), n(name) {}
int age() const { return a; }
string name() const { return n; }
};
vector<Person> ppl = { Person(12, "Tim"), Person(21, "Tom") };
vector<string> names;
// question
// copy(ppl.begin(), ppl.end(), ????.name());
for (Person& p : ppl)
names.push_back(p.name());
copy(names.begin(), names.end(), ostream_iterator<string>(cout, ", "));
How about std::transform with std::back_inserter:
std::transform(std::begin(ppl), std::end(ppl),
std::back_inserter(names),
[](const Person& p) -> std::string { return p.name(); });
In the end though, it's still basically the same as what you already have.
A range-based for loop is usually the most straightforward way to do an arbitrary operation to each element of a container. There's also the transform algorithm:
transform(
ppl.begin(),
ppl.end(),
back_inserter(names),
[](Person const & p){return p.name();}
);
vector<string> names(ppl.size());
transform(ppl.begin(), ppl.end(), names.begin(), [](const Person& p) { return p.name(); });
or for C++03:
transform(ppl.begin(), ppl.end(), names.begin(), mem_fun(&Person::name));
Related
For example I have struct
struct A {
std::string Id;
std::string Name;
Std::string Year;
};
I defined data type look like
std::map<A, int> MyMap;
I put some item to MyMap. I want to find an item that satisfy one of below
- MyMap.find(it1); //that return iter if match Id
- MyMap.find(it2); //that return iter if match both Id and Name
- MyMap.find(it3); //that return iter if match all Id, Name,Year
I know I must define operator< in struct A but how to define that work with 3 cases above. Or Which data type instead of Map is suitable in this case.
std::map can only have one predicate for associating key with a value.
You can use different predicates with the standard algorithm std::find_if to achieve this, but it does a linear search, rather than an efficient map lookup.
If you need multiple predicates to look up an element efficiently, then you need a multi-index container. The standard library does not have such a thing, but you can implement one by using multiple maps internally, or you can use a generic solution from Boost.
If your research is still in same order, you might use something like:
struct A {
std::string Id;
std::string Name;
Std::string Year;
};
bool operator < (const A& lhs, const A& rhs) {
return std::tie(lhs.Id, lhs.Name, lhs.Year) < std::tie(rhs.Id, rhs.Name, rhs.Year);
}
auto findById(const std::map<A, int>&m, const std::string& id)
{
auto it = m.lower_bound(A{id, "", ""});
if (it != m.end() && it->first.Id == id) {
return it;
}
return m.end();
}
auto findByIdName(const std::map<A, int>&m, const std::string& id, const std::string& name)
{
auto it = m.lower_bound(A{id, name, ""});
if (it != m.end() && it->first.Id == id && it->first.Name == name) {
return it;
}
return m.end();
}
auto findByIdNameYear(const std::map<A, int>&m,
const std::string& id,
const std::string& name,
const std::string& year)
{
return m.find(A{id, name, year});
}
If you prefer to use std::vector, you may use std::find_if like that:
std::vector<A> as = /*...*/;
auto it = std::find_if(as.begin(), as.end(),
[&](const A& a){ return a.Id = id && a.Name = name;} );
if (it == as.end()) {
// Not found
} else {
// use *it as matching A.
}
Can a std::map's or std::unordered_map's key be shared with part of the value? Especially if the key is non-trivial, say like a std::string?
As a simple example let's take a Person object:
struct Person {
// lots of other values
std::string name;
}
std::unordered_map<std::string, std::shared_ptr<Person>> people;
void insertPerson(std::shared_ptr<Person>& p) {
people[p.name] = p;
// ^^^^^^
// copy of name string
}
std::shared_ptr<Person> lookupPerson(const std::string& name) const {
return people[name];
}
My first thought is a wrapper around the name that points to the person, but I cannot figure out how to do a lookup by name.
For your purpose, a std::map can be considered a std::set containing std::pair's which is ordered (and thus efficiently accessible) according to the first element of the pair.
This view is particularly useful if key and value elements are partly identical, because then you do not need to artificially separate value and key elements for a set (and neither you need to write wrappers around the values which select the key).
Instead, one only has to provide a custom ordering function which works on the set and extracts the relevant key part.
Following this idea, your example becomes
auto set_order = [](auto const& p, auto const& s) { return p->name < s->name; };
std::set<std::shared_ptr<Person>, decltype(set_order)> people(set_order);
void insertPerson(std::shared_ptr<Person>& p) {
people.insert(p);
}
As an alternative, here you could also drop the custom comparison and order the set by the addresses in the shared pointer (which supports < and thus can be used directly in the set):
std::set<std::shared_ptr<Person> > people;
void insertPerson(std::shared_ptr<Person>& p) {
people.insert(p);
}
Replace set by unordered_set where needed (in general you then also need to provide a suitable hash function).
EDIT: The lookup can be performed using std:lower_bound:
std::shared_ptr<Person> lookupPerson(std::string const& s)
{
auto comp = [](auto const& p, auto const& s) { return p->name < s; };
return *std::lower_bound(std::begin(people), std::end(people), s, comp);
}
DEMO.
EDIT 2: However, given this more-or-less ugly stuff, you can also follow the lines of your primary idea and use a small wrapper around the value as key, something like
struct PersonKey
{
PersonKey(std::shared_ptr<Person> const& p) : s(p->name) {}
PersonKey(std::string const& _s) : s(_s) {}
std::string s;
bool operator<(PersonKey const& rhs) const
{
return s < rhs.s;
}
};
Use it like (untested)
std::map<PersonKey, std::shared_ptr<Person> > m;
auto sptr = std::make_shared<Person>("Peter");
m[PersonKey(sptr)]=sptr;
Lookup is done through
m[PersonKey("Peter")];
Now I like this better than my first suggestion ;-)
Here's an alternative to davidhigh's answer.
struct Person {
// lots of other values
std::string name;
}
struct StrPtrCmp {
bool operator()(const std::string* a, const std::string* b) const {
return *a < *b;
}
}
std::map<const std::string*, std::shared_ptr<Person>, StrPtrCmp> people();
void insertPerson(std::shared_ptr<Person>& p) {
people[&(p.name)] = p;
}
std::shared_ptr<Person> lookupPerson(const std::string& name) const {
return people[&name];
}
And a few edits to make it work with std::unordered_map:
struct StrPtrHash {
size_t operator()(const std::string* p) const {
return std::hash<std::string>()(*p);
}
};
struct StrPtrEquality {
bool operator()(const std::string* a, const std::string* b) const {
return std::equal_to<std::string>()(*a, *b);
}
};
std::unordered_map<const std::string*, std::shared_ptr<Person>, StrPtrHash, StrPtrEquality> people();
Consider the following:
struct A
{
int i;
double d;
std::string s;
};
std::list<A> list_A;
I'd like to copy all the elements of list_A to a map such that every pair in the map will consist of an element from list_A as value and its string s as key. Is there a way of doing it that is more elegant than looping through the list and insert each element along with its string as key to the map?
I love standard library algorithms and lambdas but it doesn't get much simpler than:
for (const A& value : list_A) {
map_A.insert(std::make_pair(value.s, value));
}
The other methods are doing the equivalent of this code and this loop is readable and just as fast.
This should get you the idea of how to use transform:
std::pair<std::string, A> pairify(const A& a) { return std::make_pair(a.s, a); }
std::transform(list.begin(), list.end(), std::inserter(map, map.end()), pairify);
The reason to use the inserter is:
An insert interator is a special type of output iterator designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements automatically at a specific position in the container.
Sorry answered too quickly last time without details, here is a compilable code.
struct A
{
int i;
double d;
std::string s;
};
std::list<A> list_A;
std::pair<std::string, A> convert(const A &x) {
return make_pair(x.s,x);
}
int main() {
std::map<std::string,A> out;
std::transform(list_A.begin(), list_A.end(), std::inserter(out,out.end()),convert);
}
I may store it in a set: in this way there would not be data duplication in the map (s itself):
struct A
{
bool operator < (const A& r_) const { return (s < r_.s); }
int i;
double d;
std::string s;
};
std::list<A> list_A;
std::set<A> set_A;
for ( std::list<A>::const_iterator itr = list_A.begin(); itr != list_A.end(); ++itr ) {
if ( ! set_A.insert(*itr).second ) {
// Handle duplicated elements
}
}
I may keep the loop: in this way you could handle duplicated elements correctly.
If you use C++11 you can use lambda function with capture:
std::map<std::string, A> m;
std::list<A> l;
std::for_each(l.begin(), l.end(),
[&](const A& a) {
m.insert(std::make_pair(a.s, a));
});
I have:
struct MyStruct
{
char* name;
int* somethingElse;
};
And I need to find in a std::vector<MyStruct*> an element (by using std::find_if) whose name is "XYZ" ... but ... the Predicate of std::find_if (if I have managed to understand it correctly) is a plain function, and it takes in a MyStruct pointer and I have no idea where I can specify the extra "XYZ" value to be used in the comparison.
So, how can I use std::find_if or this purpose? (Obviously, looking for a nice solution, not a global variable, or just walk through the list, ....)
Thanks, f
You can use a functor for this (hope I didn't get anything wrong, as I typed it in the browser):
class finder
{
const char* name;
public:
finder(const char* _name): name(_name) {}
bool operator()(MyStruct* elem) {return strcmp(elem->name, name) == 0;}
};
finder f("sample");
std::find_if(myvector.begin(), myvector.end(), f);
If you use C++11 and lambda:
std::vector<MyStruct> mystructus;
std::find_if(mystructus.begin(), mystructus.end(),
[](const MyStruct& ms){ return ms.name == std::string("XYZ"); } );
You have two options, either use functors or lamdas.
Using functors, you create a new class (or structure) whose constructor takes the string you want to search for, then it has an operator() function that is called by std::find_if:
class my_finder
{
std::string search;
public:
my_finder(const std::string& str)
: search(str)
{}
bool operator()(const MyStruct* my_struct) const
{ return search == my_struct->name; }
};
// ...
std::find_if(std::begin(...), std::end(...), my_finder("XYZ"));
The second using lambdas is less code, but requires recent version of the compiler that can handle C++11 lambdas:
std::find_if(std::begin(...), std::end(...), [](const MyStruct* my_struct)
{ return std::string("XYZ") == my_struct->name; });
The last example can even be generalized further:
using namespace std::placeholders; // For `_1` used below in `std::bind`
// Declare a "finder" function, to find your structure
auto finder = [](const MyStruct* my_struct, const std::string& to_find) {
return to_find == my_struct->name;
};
auto xyz = std::find_if(std::begin(...), std::end(...), std::bind(finder, _1, "XYZ"));
auto abc = std::find_if(std::begin(...), std::end(...), std::bind(finder, _1, "ABC"));
This way the lambda can be reused.
Predicate is anything, that can have operator () applied to it (with the expected argument(s) and returns something convertible to bool). A pointer to function is such thing, but so is an object that defines operator().
You need to provide a predicate like this:
struct Comparator
{
Comparator(const char* find) : m_find(find){}
bool operator()(MyStruct* p) const
{
return strcmp(p->name, m_find) == 0;
}
const char* m_find;
};
Then you can std::find_if like this:
vector<MyStruct*>::iterator iter = std::find_if(vec.begin(), vec.end(), Comparator("XYZ"));
if(iter != vec.end())
{
MyStruct* p = *iter;
}
Or if your compiler supports C++11 you can use lambdas and get rid of the predicate functor:
auto it = std::find_if(vec.begin(), vec.end(), [](MyStruct* p) { return strcmp(p->name, "XYZ") == 0;});
I have a little problem in C++ I don't know how to solve.
The first part of the problem is to access an element in a struct via [], or better, to map [] to a subelement.
My struct looks like this:
struct e {
std::string content;
std::string name;
std::map<std::string, std::vector<e> > elements;
};
If I want to access a subelement of e, I can do this like this: e.elements["e1"][0].elements["e1sub"][0].content, would it be possible to map this so I can call it like this: e["e1"][0]["e1sub"][0], this would just mean that he has to "replace" every e[] with e.elements[].
Maybe this can be done with templates but I don't know how to use them yet as I'm just beginning to learn C++.
Thanks in advance for any help, Robin.
You need to overload operator[]. Typically, you want to implement two versions of that operator, but since std::map only overloads the non-const version, that might be enough for you.
Something like the following should do:
struct e {
std::string content;
std::string name;
std::map<std::string, std::vector<e> > elements;
std::vector<e>& operator[](const std::string& key) {return elements[key];}
};
You can "overload" the [] operator, try:
struct e {
std::string content;
std::string name;
std::map<std::string, std::vector<e> > elements;
std::vector<e>& operator [](const std::string& s);
};
...
std::vector<e>& e::operator [](const std::string& s) {
return elements[s];
}
You don't need templates. You merely need an operator[] :
std::vector<e>& e::operator[](std::string const& s) { return this->elements[s]; }
// elements.operator[s] inserts s if it doesn't exist yet. That's non-const so the following won't work
// std::vector<e> const& e::operator[](std::string const& s) const { return this->elements[s]; }