Vector 和 for 不产生结果
Posted
技术标签:
【中文标题】Vector 和 for 不产生结果【英文标题】:Vector and for doesn't produce results 【发布时间】:2018-03-29 12:11:13 【问题描述】:我正在开发一个简单的游戏(石头、纸、剪刀),当我尝试使用另一个向量的元素作为条件来填充向量时遇到了这个问题。代码可能更容易理解:
#include "stdafx.h"
#include "../../Library/std_lib_facilities.h"
// With the fibonacci series I can generate a secret sequence of number
int fib(int n)
if (1 == n || 2 == n)
return 1;
else
return fib(n - 1) + fib(n - 2);
// It shows the list of element in a given vector
void showVector(vector<int>myVector, string nameVector)
cout << "\n\n";
cout << nameVector << " Debug: | ";
for (int i = 1; i < 10; i++)
cout << myVector[i] << " | ";
cout << "\n\n";
// String variant
void showVector(vector<string>myVector, string nameVector)
cout << "\n\n";
cout << nameVector << " Debug: | ";
for (int i = 1; i < 10; i++)
cout << myVector[i] << " | ";
cout << "\n\n";
// Generate a sequence of number in a vector
vector<int> generateCode(int input)
vector<int>myVector;
for (int i = 1; i <= 10; i++)
myVector.push_back(fib(i + input) % 3);
return myVector;
int main()
// Secret sequence of moves, based on the value (0, 1 or 2) i will show Rock, Paper or Scissor
vector<int>fibSeries;
vector<string>movSeries;
// Initialization and settings
int input = 0;
cout << "Digit an integer to start: ";
while (cin >> input)
// Check the number to make sure is a valid one (i will implement a check later) and generate the secret code
fibSeries = generateCode(input);
// For each 0 i'll put Rock in the vector, same for 1 ( in this case paper ) and 2 ( scissor )
for (int i = 0; i <= fibSeries.size(); i++)
if (fibSeries[i] == 0)
movSeries.push_back("Rock");
else if (fibSeries[i] == 1)
movSeries.push_back("Paper");
else if (fibSeries[i] == 2)
movSeries.push_back("Scissor");
else
movSeries.push_back("Rock");
// Shows the vector graphically, for debug.
showVector(fibSeries, "fibisSeries");
showVector(movSeries, "movSeries");
// So for a combination of 1 - 2 - 0 - 1
// The result should be: Paper - Scissor - Rock - Paper
return 0;
执行代码后,我得到一个(中止)。我不明白,我是 C++ 新手,所以如果是一个愚蠢的错误,请原谅我。而且大部分代码都很复杂,因为我有规则要遵循,所以例如,如果我不学习某些东西,我就不能在这里使用它。我只是想知道为什么代码不希望我将 for 与向量一起使用!
【问题讨论】:
你的错误在这里for (int i = 0; i <= fibSeries.size(); i++)
我还担心当用户输入太高时你会出现堆栈溢出(讽刺的是)。
经典围栏发布错误。 i <= fibSeries.size()
-> i < fibSeries.size()
【参考方案1】:
for (int i = 0; i
正如@Yola 提到的,你迭代的元素比你拥有的多:
如果您有向量std::vector<char> v = 'a', 'b', 'c';
,则可以通过以下方式访问元素:
v[0] (-> 'a')
v[1] (-> 'b')
v[2] (-> 'c')
通常,您的最大索引为length - 1
。
for (int i = 0; i <= fibSeries.size(); i++)
从 0 迭代到小于 或等于 到 size();即 0、1、2、3(4 个元素,在 3 个元素向量中)。
正确的代码应该是:
for (int i = 0; i < fibSeries.size(); i++)
^^^
【讨论】:
【参考方案2】:如前所述,您的问题是您应该始终使用< size
,因为索引是0-based <= size
将读取分配空间的末尾。
但是如果我可以的话,c++11 引入了一种新样式 range based for
-loops,这将使您完全不必编写条件和迭代表达式:
map<int, string> movSeries = 0, "Rock"s , 1, "Paper"s , 2, "Scissors"s ;
vector<int> fibSeries(10);
auto input = 0;
while(cin >> input)
for(auto& i : fibSeries)
i = fib(input++) % 3;
cout << "\n\nDebug | ";
copy(cbegin(fibSeries), cend(fibSeries), ostream_iterator<int>(cout, " | "));
cout << "\n\n\n\nDebug | ";
for(const auto i : fibSeries)
cout << movSeries[i] << " | ";
cout << endl << endl;
【讨论】:
以上是关于Vector 和 for 不产生结果的主要内容,如果未能解决你的问题,请参考以下文章
1002. A+B for Polynomials (25)