There is a list of items and attribues.
struct Item { double a, b; Item (double a_, double b_): a(a_), b(b_){}};
typedef std::vector <Item> TItems;
typedef std::vector <double> TAttributes;
I am trying to sort items by attributes using a list of pairs:
int main(int argc, char* argv[])
{
TItems items;
items.push_back(Item (1.0, 2.0 ));
items.push_back(Item (3.0, 4.0 ));
items.push_back(Item (5.0, 6.0 ));
TAttributes attributes;
attributes.push_back(8);
attributes.push_back(7);
attributes.push_back(9);
std::pair <TAttributes, TItems> pairs;
//No element has been coppied
std::copy (pairs.first.begin(), pairs.first.end(), std::back_inserter (attributes));
//No element has been coppied
std::copy (pairs.second.begin(), pairs.second.end(), std::back_inserter (items));
std::sort (pairs.first.begin(), pairs.first.end());
}
There are 2 problems:
A] unsuitable implementation of copying (no ellement has been coppied)
B] too “screwed” code.
Is there any more simple way how to sort list of items using another list of attributes?
How to implement copy operation correctly?
Indeed you are not putting anything in
pairs.See constructor of
std::pair.Also if you want to reorder
Itemsbased on attribute value, you probably want to callstd::sorton a list ofstd::pair<double, Items>(not a pair of list) and provide the appropriate comparison function.