STL list <>

1.how I can know that it is the end of the list ?
2.how I can find a spesific object in the STL list?
can someone give me a code?

4 example I have the class

1
2
3
4
5
class a
{
 int a;
list<int> my_list;
};


thanks in advance!!
Last edited on
1
2
3
4
5
6
7
8
9
list<int> l;
...
list<int>::iterator i = l.begin();
while(1)
{
   if(i == l.end())
      break;//end reached
   ++i;
}
2.how I can find a spesific object in the STL list?

What resembles that object? What is criteria for search?

example:
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
#include <iostream>
#include <list>
using namespace std;

class Foo
{
public:
	int data;
	Foo(int d) : data(d) 
	{
	}
	// must implement ths for containers!
	Foo(const Foo& other) 
	{ 
		*this = other; 
	}
	// must implement ths for containers!
	Foo& operator = (const Foo& other)
	{
		data = other.data;
		return *this;
	}
};

int main()
{
	const unsigned int numElements = 5;
	list<Foo> l;
	for(unsigned int i = 0; i < numElements; ++i)
	{
		l.push_back(Foo(i));
	}

	const int numberTosearch = 3;

	list<Foo>::iterator i = l.begin();
	while(1)
	{
		if(i == l.end())
		{
			cout << "Number " << numberTosearch << " not found!" << endl;
			break;
		}

		if((*i).data == numberTosearch)
		{
			cout << "Number " << numberTosearch << " found!" << endl;
			break;
		}
		++i;
	}
	
	cout << "Press enter to exit...";
	cin.get(); 
	return 0;
} 
Topic archived. No new replies allowed.