Quick question /g/
You have a container, say a list. Each object in the container has an attribute A. What you want now is a new container of the same type containing those A's. Example:
[object0(A=4), object1(A=2), object2(A=0)]
to
[4, 2, 0].
I know how to write it, question is whether there is something like this in the standard library. Does not need to work irrespective of container type.
http://en.cppreference.com/w/cpp/utility/initializer_list
>>60929169
>http://en.cppreference.com/w/cpp/utility/initializer_list
no..?
>>60927654
std::transformclass object
{
public:
object(int value) : foo(value) {};
int foo;
};
std::vector<object> from_vector;
for (int i = 0; i < 10; ++i)
{
from_vector.push_back(object(i));
}
std::vector<int> to_vector(from_vector.size());
std::transform(from_vector.begin(), from_vector.end(), to_vector.begin(), [](object const &obj) {return obj.foo; });
>>60927654
>>60931123
Exactly like that. Here's a fully working example with some extra stuff.#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct object
{
int id;
friend ostream& operator<<(ostream& os, const object& obj)
{
os << "object{" << obj.id << "}";
return os;
}
};
template <typename Cont,
typename Func,
typename OutputIterator>
void get_property(const Cont& cont, OutputIterator out, Func&& func)
{
transform(begin(cont), end(cont), out, func);
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec)
{
size_t size = vec.size();
os << '[';
for (size_t i = 0; i < size; i++)
{
os << vec[i];
if (i + 1 != size)
os << ", ";
}
os << ']';
return os;
}
int main()
{
const unsigned N = 10;
vector<object> objs(N);
generate_n(objs.begin(), N, [id=0]() mutable {
return object{id++};
});
vector<unsigned> ids(objs.size());
get_property(objs, ids.begin(), [](auto&& obj) { return obj.id; });
cout << objs << '\n';
cout << ids << '\n';
return 0;
}
It's just a wrapper aroundstd::transformreally.
>>60927654
>inb4 some NEET comes and says that C++ is horrible
>>60932347
C++ is a horrible choice for most project that use it. Go use python. Or Java. Or Haskell.
The amount of web applications (handling sensitive information) that are coded in C++ by 16yos is too fucking high.
>>60932473
This is true, using C++ is like fire juggling or doing anything on psychedelics. It can go wrong easily and you should know what you are doing. But there is potential for profit if you give it the necessary respect and time. But if efficiency is not an issue don't use it, I say.for (Class &object : objects) {
second_Container.push_back(object.attribute);
}
is what I ended up using before your replies.
>>60933065
Last sentence does not fit metaphor. Also my indentation sucks.
Another one (don't need a new thread for every question amirite)
Why does this give the error it gives?template <size_t girth> struct A {
struct B {
attribute = 0;
};
struct C : B {
std::array<double, girth> someOtherStuffUsingTemplateParameter;
attribute = 3; // error: 'attribute' was not declared in this scope
};
};
>>60933281
yeah line 4 should readint attribute = 0;
>>60932347
It does, it's atrocious.
That's why Rust is infinitely better than this piece of shit. Hell, even Go would be a better choice not even kidding.
>>60932312
Nice and sweet
>>60927654
stupid frog poster