sort algorithm

hey guys am trying to arrange my values from smallest to biggest using vector....if the value in position 0 is greater than the value at position one than swap the two than reapeats for value at position 1 and that at position 2.so on and so on..the algorithm goes on until all values in the vector have been compared...bt my problem is that code crushes...sayin out of range. please help..plus am i using pointers in a vector the right way?.....thanx in advance

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
#include <iostream>
#include <vector>
#include <string>

using namespace std;


int main()
{

    vector<int>* num = new vector<int>();
    int x=0;
    cout<<"please enter range of number and -1 to quit"<<endl;
    while(cin>>x&&x!=-1)
    {
        num->push_back(x);
    }

    for(int i=0; i<num->size(); i++)
    {
        cout<<num->at(i);
    }

    cout<<endl;

    for(int i=0; i<num->size(); i++)
    {
        if(num->at(i)>=num->at(i+1))
        {
            num->at(i)=num->at(i+1);
            cout<<"yes"<<endl;
        }
        cout<<num->at(i);
    }

    cout<<endl;
}
Last edited on
1
2
3
    for(int i=0; i<num->size(); i++) //last iteration: i=size-1
    {
        if(num->at(i)>=num->at(i+1)) //last iteration: at(size-1) >= at(size), invalid 



> than reapeats for value at position 1 and that at position 2.so on and so on
¿so on how?

> swap the two
num->at(i)=num->at(i+1); is not swapping, just assignment.


> am i using pointers in a vector the right way?
no.
I want to understand your thought process
int x=0; there you declared an integer variable and initialize it.
But for some reason that was too complicated for a vector, so you come up with vector<int>* num = new vector<int>();
http://www.cplusplus.com/forum/general/138037/
Topic archived. No new replies allowed.