Vector's build in functions

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

using namespace std;
template <typename T>
	void printVector(vector<T> v1)
	{
		for (int i = 0; i<v1.size(); i++)
		{
			cout<<v1.at(i)<<endl;
		}
	}
int main(void)

{
	
	int xxx;
	double x;
	vector<double> myVec;
	cout<<"what is the size of array you want to input?";
	cin>>xxx;
	for(int i = 0; i <xxx; i++)
	{
		cout<<"enter your "<<i+1<<" value";
		cin>>x;
		myVec.push_back(x);
	}
	printVector(myVec);
	cout<<endl;
	cout<<"the size and capacity of the vecotr is  "<<myVec.size()<<" and "<<myVec.capacity();
	
	 


}


I have been studying vectors and it's awesome i just want to know is there a build in function/algorithm in vector that can display my values in the vector in descending/ascending order?
not part of vector, i don't think, but if you #include <algorithm> you can use sort

http://www.cplusplus.com/reference/algorithm/sort/
i tried to modify my code but it's not working you know why?
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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {

	int size;
	double x;
	vector<double> myVec;
	cout<<"what is the size of array you want to input?";
	cin>>size;
	for(int i = 0; i <size; i++)
	{
		cout<<"enter your "<<i+1<<" value";
		cin>>x;
		myVec.push_back(x);
	}
  vector<int> myvector (myVec, myVec+(myVec.size());          
  vector<int>::iterator it;

  // using default comparison (operator <):
  sort (myvector.begin(), myvector.begin()+((myVec.size())/2));           

  // using function as comp
  sort (myvector.begin()+((myVec.size())/2), myvector.end(), myfunction);

  // using object as comp
  sort (myvector.begin(), myvector.end(), myobject);     

  // print out content:
  cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;

  return 0;
}

the problem is line 26, how to fix it?
vector<int> myvector (myVec.begin(), myVec.end());
I don't really understand the whole concept about sorting, i am trying to code for descending order. Normally for an array you change the for loop but in this case i need to do something about the sort, what are the things i need to do?
??
?? anyone can help m ehere?>
Topic archived. No new replies allowed.