# Selection Sort problem #2

This is a discussion on Selection Sort problem #2 within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've had a similar topic before - but this isn't about the fundamental workings of Selection Sort this ...

1. ## Selection Sort problem #2

Hey guys, I've had a similar topic before - but this isn't about the fundamental workings of Selection Sort this time.

I am using vectors in correspondence with the selection sort algorithm. (Just for fun and games)...However, I'm getting an annoying error that I cannot think of why it's happening (Because I changed the code around a bit - but this line is deciding to act up now...When it's fundamentally the same as it was before).

Anyhoo - here's the code.

Code:
```#include <iostream>
#include <vector>

using namespace std;

int main()
{//Main
vector<int> Selected_Numbers;
int number;
bool quit = false;
int decision;

while(!quit)
{//While
cout<<"(1)Enter a number, or (2)quit, or (3)continue"<<endl;
cin >>decision;

switch (decision)
{//Switch
case 1:

cout<<"What number is it?"<<endl;
cin >>number;
Selected_Numbers.push_back(number);
break;

case 2:

quit = true;
break;

case 3:

int LowestNumber = 0;
int CurrentNumber = 0;

for(vector<int>::iterator iter = Selected_Numbers.begin();
iter != Selected_Numbers.end(); ++iter)
{//For1

CurrentNumber = *iter;

if(LowestNumber <= CurrentNumber)
{//If1
LowestNumber = CurrentNumber;

Selected_Numbers.begin() = LowestNumber;
/*^This is the line generating
the problem. Compiler says
'error C2679: binary '=' :
no operator found
which takes a right-hand
operand of type 'int'
(or there is no acceptable
conversion)' */

}//Close if1
else
{//else1
cout<<"Next Number...."<<endl<<endl;
}//close else1
}//close For1

cout<<"Alrighty then...All has been arranged from lowest - highest"<<endl<<endl;

for(vector<int>::iterator iter = Selected_Numbers.begin();
iter != Selected_Numbers.end(); ++iter)
{//For2
cout<<*iter<<endl;
}//close For 2
break;
}//Close Switch

}//Close While

system("pause");
}//Close Main```
Any help will be much appreciated....I do understand what the error says - but I don't know why it's saying it! Cos before, it worked exactly the same (I had to make a change to the code to get Selection Sort to work, by adding the variable 'current number').

- Twigstar

2. Try the following it work on my IDE/Compiler

Code:
`#include <algorithm>`
and then

Code:
`sort(Selected_Numbers.begin(), Selected_Numbers.end());`

3. Oh, this is just personal practice...I wouldn't want to use a predefined method....Although it's nice to know there is one!

4. Possibly this might help

Code:
```vector<int> myIV;
myIV[0] = 10;
myIV.push_back(20);```

5. If you are trying to assign the LowestNumber to the first entry in the vector, then you want this:

*(Selected_Numbers.begin()) = LowestNumber;

This will fix your compiler error because you dereference the iterator so that you aren't assigning to it, but your selection sort algorithm will still need some work.

6. You're right in both cases. It works now and it doesn't.

Hmm, I'll ponder on this one.

Also, I wanted to use an Iterator, and not subscripting - again, for my own personal practice - thanks anyway!

7. here one i wrote to sort a regualr array
maybe you can use it to better understand
how to sort the vector

Code:
```char alphabet[4];
char check;
for(int i = 0; i != 3; i++)
{
for(int j = 0; j != 2; j++)
{
if(alphabet[j] > alphabet[j+1])
{
check = alphabet[j];
alphabet[j] = alphabet[j+1];
alphabet[j+1] = check;
}
}
}```

8. Sorry to double post...Wasn't letting me edit for some bizarre reason.

Anyhoo, I slapped myself when I realised this piece of code:

Code:
`if(LowestNumber <= CurrentNumber)`
lol! I can be quite silly sometimes. So my new code switched them around...And then we have more problems! Here's the 'Selection Sort' part of the code.

Code:
```for(vector<int>::iterator iter = Selected_Numbers.begin();
iter != Selected_Numbers.end(); ++iter)
{//For1

CurrentNumber = *iter;

if(CurrentNumber <= LowestNumber)
{//If1
LowestNumber = CurrentNumber;

*(Selected_Numbers.begin()) = LowestNumber;
}//Close if1
else
{//else1
cout<<"Next Number...."<<endl<<endl;
}//close else1```
It works better than before...Cos before, I typed in '3, 2, 1' - and the sort turned it into '3, 2, 1'.....But now, I put in the same - and for some bizzare reason, it returns '1, 2, 1'.....

I can't seem to see where LowestNumber would pick up '1' again...And also omit the three....

Any pointers?

...[It worked!]

Pointer(iterator) arithmitic! Damn...So I just check the next element up in the same go, compare it, and then swap if true. And then +1 to the iter...Ok, I'll give it a bash - cheers 'IloveVectors'(What an appropiate name for this situation, lol)...I'd still like to know how that 1 got there instead of the three though, lol