在C++中,排序向量,列表或任何集合,我會用:如何在C++中使用C#委託像functor?
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
vector<int> vt;
vt.push_back(3);
vt.push_back(1);
vt.push_back(2);
sort(vt.begin(), vt.end(), greater<int>());
}
在C#中,我發現List<>
相當於std::vector<>
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Professional_Csharp {
class Program {
static void Main(string[] args) {
List<int> intList = new List<int>();
intList.Add(3);
intList.Add(2);
intList.Add(1);
intList.Sort();
}
}
}
這工作得很好,但是如果我想定製比較器,我怎麼能實現呢?或者,如果我想排序一個特定的範圍而不是整個列表?我怎麼能這樣做?
更新
sort(vt.begin(), vt.begin() + 1);
是否有可能在C#?
感謝,
陳
我建議你擺脫C++代碼的問題;你在最後提出的問題是足夠具體的。 – Justin 2011-02-04 19:32:38