Finding a member in a struct

How do you find a member in a vector of a struct? I want to find a particular member name. After I find it I want to delete it.

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
  //	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 (auto n : vi)
		auto p = std::find(vi.begin(), vi.end(), [](std::vector<Item>& vi) 
			{return vi.name == "Senator"; });  // error is here
}
	
Last edited on
1
2
3
4
5
6
7
//for (auto n : vi){ //¿why a loop?
auto p = std::find_if( //find() ask for a value, use find_if() for condition
	vi.begin(),
	vi.end(),
	[](const Item& vi) //you want to compare an item
		{return vi.name == "Senator"; }
); 
Last edited on
1
2
3
4
5
6
7
8
9
	// for (auto n : vi)
		// auto p = std::find(vi.begin(), vi.end(), [](std::vector<Item>& vi) 
			// {return vi.name == "Senator"; });  // error is here

	const std::string the_name =  "Senator" ;
        const auto p = std::find_if( vi.begin(), vi.end(),
                                     [the_name] ( const Item& a ) { return a.name == the_name ; } ) ;
                                      // note: the_name is captured by value
        if( p != vi.end() ) vi.erase(p) ;
And if there might be several entries with the same name and you want to delete all of them, use erase-remove idiom:
1
2
3
4
const std::string the_name =  "Senator" ;
vi.erase(std::remove_if(vi.begin(), vi.end(), 
                        [the_name](const Item& e){ return return e.name == the_name;} ),
         vi.end())
Last edited on
Thank you all. I tried the three solutions, and all three work. I came up with the following code:
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';
}


I chose to use vi.erase(std::remove_if... in the event there may be several entries with the same "name" in some future application. Most of what I have done so far is unique names.

Topic archived. No new replies allowed.