[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

c++ extracting container object attribute containers

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 2

File: 8477_b6fe_500.jpg (32KB, 500x496px) Image search: [Google]
8477_b6fe_500.jpg
32KB, 500x496px
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::transform

class 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 around
std::transform
really.
>>
>>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.
>>
File: 4VSTYoA.jpg (26KB, 629x294px) Image search: [Google]
4VSTYoA.jpg
26KB, 629x294px
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 read
int 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
Thread posts: 14
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.