我在C++下面的代碼:矢量擦除錯誤
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
int main()
{
srand(time(0));
int noOfElements = 9;
for (int a = 0; a < 9; a++)
{
std::vector<int> poss;
for (int a = 1; a <= 9; a++)
poss.push_back(a);
for (int b = 0; b < 9; b++)
{
int random = rand() % 9;
std::cout << poss[random];
poss.erase(random);
noOfElements--;
}
std::cout << "\n";
}
}
然而,當我運行它,它返回:
error: no matching function for call to 'std::vector<int>::erase(int&)'
線路13
這是爲什麼和我該如何糾正它?
http://en.cppreference.com/w/cpp/container/vector/erase – hmjd 2013-04-25 15:39:34
你試圖抹掉隨機位置,或者隨機值?它有很大的不同。 – john 2013-04-25 15:40:36
如果使用標準算法,這將會更短,更穩健。 'std :: iota'代替你的第一個內部循環,'std :: random_shuffle'洗牌它們。 'std :: copy'可以輸出它們,'std :: vector :: clear'可以重置向量。 – chris 2013-04-25 15:41:46