在C#给出一个结构:
public class MainActivity : Activity
{
ProgressBar pb;
TextView tv;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
pb = FindViewById<ProgressBar>(Resource.Id.pb);
tv = FindViewById<TextView>(Resource.Id.tv);
UpdatePB uptask = new UpdatePB(this,pb,tv);
uptask.Execute(100);
}
public class UpdatePB : AsyncTask<int, int, string>
{
Activity mcontext;
ProgressBar mpb;
TextView mtv;
public UpdatePB(Activity context,ProgressBar pb,TextView tv) {
this.mcontext = context;
this.mpb = pb;
this.mtv = tv;
}
protected override string RunInBackground(params int[] @params)
{
// TODO Auto-generated method stub
for (int i = 1; i <= 4; i++)
{
try
{
Thread.Sleep(3000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
Android.Util.Log.Error("lv",e.Message);
}
mpb.IncrementProgressBy(25);
PublishProgress(i * 25);
}
return "finish";
}
protected override void OnProgressUpdate(params int[] values)
{
mtv.Text = String.ValueOf(values[0]);
Android.Util.Log.Error("lv==", values[0] + "");
}
protected override void OnPostExecute(string result)
{
mcontext.Title = result;
}
}
}
我们可以这样写:
struct Point {int x, int y}
如何使用lambda函数在C ++中表达这个逻辑?
答案 0 :(得分:4)
我觉得你想在(y, x)
1 上进行词典排序。您可以利用库函数std::tie
。那个返回一个引用元组,而std::tuple
有一个小于运算符,它执行字典比较。因此,您只需要指定比较项目的顺序。
以下是它如何查找std::vector
(您在C ++中使用容器类型,始终以std::vector
开头):
std::vector<Point> my_list;
// Fill my_list
std::sort(begin(my_list), end(my_list), [](auto const& l, auto const& r){
return std::tie(l.y, l.x) < std::tie(r.y, r.x);
});
<子> 1 - 我只是基于方法名称,所以这可能不就是你真正追求的。 子>
答案 1 :(得分:1)
您可以使用STL函数std :: sort。例如:
void CMFCApplication2Dlg::log2File(CString newData) {
//Open or Create new log file
if (!(file.Open(_T(FILE_NAME), CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite)))
{
AfxMessageBox(_T("Couldn't open file."));
return;
}
file.Close();
CString oldData;
//read binary data from file --> decrypt it and return decrypted oldData.
readFile(oldData);
//Add at the end of file new data to be encrypted.
oldData += newData;
newData = oldData;
CByteArray arBytes;
//Encypt new data
//Derive a key from a password.
crypto.DeriveKey(CRYPTO_PASS);
//put encypted newData to arBytes.
crypto.Encrypt(newData, arBytes);
//Delete file (we will write a new one ==> overwite)
CFile::Remove(_T(FILE_NAME));
//Create new log file
if (file.Open(_T(FILE_NAME), CFile::modeNoTruncate | CFile::modeCreate |CFile::modeReadWrite))
{
//Write the encrypted data (byte array) to a "new" file
//(we deleted the original one and creating a new one with the same name (overwrite)
file.Write(arBytes.GetData(), static_cast<UINT>(arBytes.GetCount()));
file.Close();
}
else {
AfxMessageBox(_T("Couldn't write newData to file."));
}
//For Debug only ==> popup the file content
CString str1;
readFile(str1);
AfxMessageBox((str1));
}
void CMFCApplication2Dlg::readFile(CString &str) {
//Open the file in read mode
if ( (file.Open(_T(FILE_NAME), CFile::modeRead) == TRUE) &&
(file.GetLength() == 0) )
{
//There is no file ==> first time
//Nothing to read, return empty string
file.Close();
str = "";
return;
}
CByteArray arBytes;
CString m_strData;
//Size the array to accomodate the file bytes.
arBytes.SetSize(static_cast<INT_PTR>(file.GetLength()));
// Copy the data and close the file.
file.Read(arBytes.GetData(), static_cast<UINT>(file.GetLength()));
file.Close();
// Try and deserialize the data.
//Derive a key from a password.
crypto.DeriveKey(CRYPTO_PASS);
if (crypto.Decrypt(arBytes, m_strData) == false)
AfxMessageBox(_T("Coudln't decrypt data- check password."));
else
{
//We have data !!
str = m_strData;
}
}
答案 2 :(得分:1)
两种方式 - 要么排序两次,先按y排序,然后在x上使用稳定排序(请注意,这与C#完全相反!)。但std::sort
运气不好,因为它不稳定,幸运的是,正如本杰明暗示的那样,std::stable_sort
也是......
另一种方法是确保两点比较,以便x中的差异首先适用,并且仅在相等的情况下,y被视为:
std::sort
(
// range to sort
list.begin(), list.end(),
// next the comparator - you wanted a lambda? OK, here it is:
[](point const& a, point const& b)
{
return a.x < b.x || a.x == b.x && a.y < b.y;
}
);
答案 3 :(得分:0)
您只需要将lambda函数指定为std::list::sort()
。您可以决定如何排序。
list.sort([](Point i,Point j)
{
if(i.x!=j.x)
return i.x<j.x;
else
return i.y<j.y;
}
);