在下面的程序中,我有一個帶有非靜態函數void add()
的類A
。我想使用迭代器爲集合中的每個元素調用add()
,但最後一個元素有錯誤。使用迭代器調用STL中的非靜態函數設置
我該如何解決?包含在set<>
容器
#include <iostream>
#include <set>
using namespace std;
class A
{
private:
int a;
public:
A(int x) { a = x; }
void add() {a = a + 1; }
bool operator<(A x) const { return a < x.a; }
};
int main()
{
//type of the collection
typedef set<A> IntSet;
//define a IntSet type collection
IntSet col1;
IntSet::iterator pos;
//insert some elements in arbitrary order
col1.insert(A(3));
col1.insert(A(4));
col1.insert(A(5));
//iterator over the collection and print all elements
for(pos = col1.begin(); pos != col1.end(); ++pos)
{
(*pos).add();
// ERROR!: Member function 'add' not viable:
// 'this' argument has type'const value_type'
// (aka 'const A'), but function is not marked const
}
cout << endl;
}
在一個'std :: set'中,你不允許**改變內容(這會弄亂順序)。 '* pos'給你一個'const A',因此你不能在它上面調用非''contst'函數'add()'。如果您想要更改存儲的「A」,則需要使用不同的容器。 – BoBTFish
您不能修改set <>,因此所有集合迭代器都是常量。 http://stackoverflow.com/questions/5632079/why-does-stdset-insert-return-a-non-const-iterator-and-yet-i-cannot-modify – alexrider
'col1.insert(* new A(5 ));'導致內存泄漏,你要查找的語法是'col1.insert(A(5));',即「調用」類名來創建它的一個實例。 –