ページ 1 / 1
ラムダ式での比較演算子
Posted: 2013年3月18日(月) 01:33
by Sx1752
コード:
#include <cstring>
class Item
{
private:
int id;
char name[32];
public:
Item(const int id = 0, const char *name = nullptr)
{
this->id = id;
if(!name)
::strcpy(this->name, "");
else
::strcpy(this->name, name);
}
const int getId() const
{
return id;
}
const char* getName() const
{
return name;
}
bool operator<(const Item another) const
{
return this->id < another.id;
}
bool operator>(const Item another) const
{
return this->id > another.id;
}
};
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<Item> ItemVector;
int main(int argc, char **argv)
{
ItemVector itemVector;
itemVector.push_back(Item(2, "アイテム2"));
itemVector.push_back(Item(4, "アイテム4"));
itemVector.push_back(Item(1, "アイテム1"));
itemVector.push_back(Item(3, "アイテム3"));
itemVector.push_back(Item(0, "アイテム0"));
stable_sort(itemVector.begin(), itemVector.end(), [](const Item& a, const Item& b) { return a < b; } );
for(ItemVector::iterator iter = itemVector.begin();
iter != itemVector.end();
++iter) {
printf("%d: %s\n", iter->getId(), iter->getName());
}
return 0;
}
アイテムクラスを作ってstable_sortしたいのですが、
コンパイル時に比較演算子をconstメンバにしないとコンパイルできないのは何故なのでしょうか。
Re: ラムダ式での比較演算子
Posted: 2013年3月18日(月) 07:48
by beatle
お示しのコードはコンパイルに失敗するコードですか?それとも成功するコード?
試しにIdeoneでコンパイルしてみたら、成功しました。
http://ideone.com/xzvz0G
Re: ラムダ式での比較演算子
Posted: 2013年3月18日(月) 07:56
by みけCAT
コード:
[](const Item& a, const Item& b) { return a < b; }
ここの引数の宣言がconstだからじゃないでしょうか?
Re: ラムダ式での比較演算子
Posted: 2013年3月18日(月) 14:00
by Sx1752
beatle さんが書きました:お示しのコードはコンパイルに失敗するコードですか?それとも成功するコード?
ここに貼ったのは成功するコードです。
みけCAT さんが書きました:コード:
[](const Item& a, const Item& b) { return a < b; }
ここの引数の宣言がconstだからじゃないでしょうか?
どうやらそのようですね。ラムダ式の
コード:
[](Item& a, Item& b) { return a < b; }
を
コード:
[](Item a, Item b) { return a < b; }
に変更するとコンパイルできました。
しかし、a, bを参照型にするとやっぱりコンパイル出来ないです。
► スポイラーを表示
コード:
#include <cstring>
class Item
{
private:
int id;
char name[32];
public:
Item(const int id = 0, const char *name = nullptr)
{
this->id = id;
if(!name)
::strcpy(this->name, "");
else
::strcpy(this->name, name);
}
const int getId() const
{
return id;
}
const char* getName() const
{
return name;
}
bool operator<(const Item& another) /* const */
{
return this->id < another.id;
}
bool operator>(const Item& another) /* const */
{
return this->id > another.id;
}
};
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<Item> ItemVector;
int main(int argc, char **argv)
{
ItemVector itemVector;
itemVector.push_back(Item(2, "アイテム2"));
itemVector.push_back(Item(4, "アイテム4"));
itemVector.push_back(Item(1, "アイテム1"));
itemVector.push_back(Item(3, "アイテム3"));
itemVector.push_back(Item(0, "アイテム0"));
stable_sort(itemVector.begin(), itemVector.end(), [](Item a, Item b) { return a < b; } );
for(ItemVector::iterator iter = itemVector.begin();
iter != itemVector.end();
++iter) {
printf("%d: %s\n", iter->getId(), iter->getName());
}
return 0;
}
Re: ラムダ式での比較演算子
Posted: 2013年3月18日(月) 14:23
by h2so5
ソート中に a, b を変更してはいけないので、コンパイルできないのは仕様です。
Re: ラムダ式での比較演算子
Posted: 2013年3月18日(月) 14:35
by Sx1752
h2so5 さんが書きました:ソート中に a, b を変更してはいけないので、コンパイルできないのは仕様です。
仕様でしたか。お答えいただきありがとうございました。