1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
// C21Drill Vector
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
struct Item {
std::string name;
int iid;
double value;
/*...*/
};
int main()
{
std::vector<Item> vi = { // Initialize with 10 Items
{ "Corvallis", 1, 195.99 },
{ "Senator", 2, 230.45 },
{ "Tankini", 3, 49.99 },
{ "Skirtini", 4, 46.99 },
{ "Sarong", 5, 59.99 },
{ "Bandeau", 6, 50.99 },
{ "Ruched", 7, 39.99 },
{ "Pintucked", 8, 42.99 },
{ "Hoodie", 9, 34.99 },
{ "Metallic", 10, 11.99 }
};
vi.push_back({ "Horse shoe", 99, 12.14 }); // add an Item
vi.push_back({ "Canon S400", 9988, 499.95 }); // add another Item
std::sort(vi.begin(), vi.end(), [](const Item& a, const Item& b) // sort alphabetically by name
{return a.name < b.name; });
for (auto n : vi)
std::cout << n.name << '\n';
vi.pop_back(); // delete last Item
vi.erase(vi.begin() + 2); // delete third Item
std::cout << '\n';
for (auto n : vi)
std::cout << n.name << '\n';
for (std::string the_name : {"Senator", "Horse shoe", "Saskat", }) {
vi.erase(std::remove_if(vi.begin(), vi.end(),
[the_name](const Item& e) {return e.name == the_name; }),
vi.end());
}
std::cout << '\n';
for (auto n : vi)
std::cout << n.name << '\n';
}
|