Iterator help

I cant seem to get this piece of code to work properly i am not sure how the iterators work
i am looking for the smallest number but i can't seem to get 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
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <iostream>
#include <string>
#include <list>
using namespace std;


void sort(list<int>&);
int main()
{
	list<int> numbers;
	numbers.push_back(3);
	numbers.push_back(6);
	numbers.push_back(5);
	numbers.push_back(9);

	

	sort(numbers);

	list < int>::iterator pos = numbers.begin();

	for (pos = numbers.begin(); pos != numbers.end(); pos++)
	{
		cout << *pos;
		cout << endl;
	}
	cout << "ran:" << endl;


	return 0;

}

void sort(list<int>& numbers)
{
	list < int>::iterator pos = numbers.begin();
	list < int>::iterator second;
	list < int>::iterator third;
	int smallest = *pos; // the first position of the list is the smallest so far

	int counter = 0; // counter is used to swap locations with the pointer when the smallest is found
	
	int total = numbers.size();
	pos++;
	second = pos;
	while (counter < total)
	{
		for (pos = numbers.begin(); pos != numbers.end(); pos++) // loop to go through the list
		{
			int current = *pos;
			cout << current << smallest << endl;

			if (current < smallest);// if the current values is smaller then the current smallest that is now the smallest
			{
				smallest = current;
			}
			cout << smallest << endl;
		}
	
		counter++;
	}
}
 
You have a semicolon after your if statement on line 54; you probably do not intend it to be there. It is causing 55~57 to be always executed rather than being the body of the if statement.
Why not just use std::sort provided by STL?
Topic archived. No new replies allowed.