回答
下面是一个使用两种不同类型谓词的示例的另一适配。指定的谓词可以是一个函数指针或一个函数,它是一个定义operator()的类,以便实例化时的对象可以像函数一样使用。请注意,我必须在功能标题中再添加一个标题包含。这是因为函数继承自std库中定义的binary_function。
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class MyData
{
public:
static bool compareMyDataPredicate(MyData lhs, MyData rhs) { return (lhs.m_iData < rhs.m_iData); }
// declare the functor nested within MyData.
struct compareMyDataFunctor : public binary_function<MyData, MyData, bool>
{
bool operator()(MyData lhs, MyData rhs)
{
return (lhs.m_iData < rhs.m_iData);
}
};
int m_iData;
string m_strSomeOtherData;
};
int main()
{
// Create a vector that contents elements of type MyData
vector<MyData> myvector;
// Add data to the vector
MyData data;
for(unsigned int i = 0; i < 10; ++i)
{
data.m_iData = i;
myvector.push_back(data);
}
// shuffle the elements randomly
std::random_shuffle(myvector.begin(), myvector.end());
// Sort the vector using predicate and std::sort. In this case the predicate is a static
// member function.
std::sort(myvector.begin(), myvector.end(), MyData::compareMyDataPredicate);
// Dump the vector to check the result
for (vector<MyData>::const_iterator citer = myvector.begin();
citer != myvector.end(); ++citer)
{
cout << (*citer).m_iData << endl;
}
// Now shuffle and sort using a functor. It has the same effect but is just a different
// way of doing it which is more object oriented.
std::random_shuffle(myvector.begin(), myvector.end());
// Sort the vector using predicate and std::sort. In this case the predicate is a functor.
// the functor is a type of struct so you have to call its constructor as the third argument.
std::sort(myvector.begin(), myvector.end(), MyData::compareMyDataFunctor());
// Dump the vector to check the result
for (vector<MyData>::const_iterator citer = myvector.begin();
citer != myvector.end(); ++citer)
{
cout << (*citer).m_iData << endl;
}
return 1;
}
std::sort(v.begin(), v.end(), predicate);
其中predicate
是一个具有以下属性的函数:
用户定义的谓词函数对象,用于定义由排序中的连续元素满足的比较条件。二元谓词有两个参数,当满足时返回
true
,当不满意时返回false
。该比较函数必须对序列中的一对元素施加严格的弱排序。
(MSDN)
例如为:
bool strings_lt_by_first_char(std::string const &x, std::string const &y)
{
return x[0] < y[0];
}
添加到这一点,你也可以离开了谓词如果你定义了“经营者<”为包含在里面向量类。 – 2011-03-29 05:30:51
// alg_sort.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>()
#include <iostream>
// Return whether first element is greater than the second
bool UDgreater (int elem1, int elem2)
{
return elem1 > elem2;
}
int main()
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for (i = 0 ; i <= 5 ; i++)
{
v1.push_back(2 * i);
}
int ii;
for (ii = 0 ; ii <= 5 ; ii++)
{
v1.push_back(2 * ii + 1);
}
cout << "Original vector v1 = (" ;
for (Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++)
cout << *Iter1 << " ";
cout << ")" << endl;
sort(v1.begin(), v1.end());
cout << "Sorted vector v1 = (" ;
for (Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++)
cout << *Iter1 << " ";
cout << ")" << endl;
// To sort in descending order. specify binary predicate
sort(v1.begin(), v1.end(), greater<int>());
cout << "Resorted (greater) vector v1 = (" ;
for (Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++)
cout << *Iter1 << " ";
cout << ")" << endl;
// A user-defined (UD) binary predicate can also be used
sort(v1.begin(), v1.end(), UDgreater);
cout << "Resorted (UDgreater) vector v1 = (" ;
for (Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++)
cout << *Iter1 << " ";
cout << ")" << endl;
}
我要求一个类,但你提供的整数数据类型样本 – karthik 2011-03-29 05:22:59
无论如何,改变谓词... – user672117 2011-03-29 05:25:05
它对于迭代器来说,最好使用prefix ++ – snoofkin 2011-03-29 10:15:12
- 1. 字符串排序 - std :: set或std :: vector?
- 2. 由内部的元素排序std :: vector?
- 3. 如何通过不同的std :: vector的值对std :: vector进行排序?
- 4. 使用std ::排序找到一个std前N项:: vector的
- 5. C++一个包含多种类型的模板类的std :: vector
- 6. 使用STL的排序功能排列std :: vector <myclass>
- 7. std ::排序获取std :: bad_alloc
- 8. 对另一个类包含的std :: vector的访问
- 9. std ::使用std :: vector :: reserve时,排序顺序是从最大到最小的?
- 10. std :: unique_locks的std :: vector向量
- 11. 正确的方式来实现std :: vector的std :: vector的类的移动语义
- 12. 是否有可以合并和排序的std :: vector的后代?
- 13. std :: vector <std :: vector <T>> vs std :: vector <T*>
- 14. 类型'std :: vector <Object*>&'&'&'
- 15. C++友元类的std :: vector的
- 16. C++模板包装类的std :: vector的
- 17. 使用包含std :: unique_ptr的std :: vector构造一个类时出现错误
- 18. 擦除std :: vector的元素是否保留了排序?
- 19. 如何仅对std :: vector中的子集进行排序?
- 20. C++ - 排序std :: vector中的元素索引
- 21. std :: vector vs std :: insert
- 22. 征和std :: vector的
- 23. 使用std :: vector的
- 24. 包含std :: strings和std :: vector的地图<Another_Struct>
- 25. iterate std :: vector <std :: vector <char>>?
- 26. 类似std :: map或std :: vector类的构造函数
- 27. 用更合适的类代替`std :: vector`
- 28. 转换boost :: variant类型的std :: vector
- 29. 导出std :: Vector类中的成员
- 30. 基本类型std :: vector的默认值
compareMyDataFunctor函数工作正常,但该静态函数不能正常工作...无论如何,我得到的解决方案 – karthik 2011-03-29 05:26:19