65,186
社区成员




x = {1,3,5.5};
y = {1,2,3.5};
z = {2,2,4.5};
//could be very big
struct Data
{
};
std::vector<Data*> v; //since sizeof(Data) could be quite big, we keep Data* instead Data
//function to compare Data. You have to give a correct implemention
bool comp(Data const* p1, Data* const p2);
//revesve memory to reduce the times of memory allocation. you should adjust this number for your case
v.reserve(10000);
//collecting data....
v.push_back(new Data);
std::sort(v.begin(), v.end(), comp);
//都给你搬出来
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
//Sort elements in range
Sorts the elements in the range [first,last) into ascending order.
The elements are compared using operator< for the first version, and comp for the second.
Elements that would compare equal to each other are not guaranteed to keep their original relative order.
例子:
#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 myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
//先自定义一个结构体
struct Test {
int member1;
int member2;
};
//自定义排序函数
bool SortByM1( const Test &v1, const Test &v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致
{
return v1.member1 < v2.member1;//升序排列
}
void MyPushback(std::vector<Test> & vecTest, const int &m1, const int &m2)
{
Test test;
test.member1 = m1;
test.member2 = m2;
vecTest.push_back(test);
}
void PrintVector( std::vector<Test> & vec)
{
/*
插一句,
vec.begin()对应的位置是向量的第一个位置,
vec.end()对应的是vector中的最后的一个元素位置的后面的一个位置(我认为,实际上是一个无效位置)
文档上的定义:Returns an iterator referring to the past-the-end element in the vector container.
*/
for(std::vector<Test>::iterator it = vec.begin() ; it != vec.end() ; it++ )
{
std::cout<<it->member1<<'\t'<<it->member2<<std::endl;
}
}
int main(int argc, char* argv[])
{
std::vector<Test> vecTest;
MyPushback(vecTest,9,1);
MyPushback(vecTest,8,2);
MyPushback(vecTest,7,3);
MyPushback(vecTest,6,4);
MyPushback(vecTest,5,5);
MyPushback(vecTest,4,6);
MyPushback(vecTest,3,7);
MyPushback(vecTest,2,8);
MyPushback(vecTest,1,9);
//排序之前
std::cout<<"Before Sort:"<<std::endl;
PrintVector(vecTest);
std::cout<<"对向量中的所有元素按member1进行升序排列:"<<std::endl;
std::sort(vecTest.begin(),vecTest.end(),SortByM1);
PrintVector(vecTest);
//std::cout<<"对向量中的第2个到第5个元素按member1进行升序排列:"<<std::endl;
//std::sort(vecTest.begin()+1,vecTest.begin()+5,SortByM1);//vecTest.begin()+5为第6个位置
//PrintVector(vecTest);
return 0;
}