创建一维向量并通过参考C+赋值+; 我是新手程序员,只需几个月就可以学习C++。这是我关于堆栈溢出的第二个问题,我真的希望这些答案能让我和其他人受益
我开始研究这个程序是因为我想创建一个向量引擎,它可以创建一个一维向量,为该一维向量中的任何位置赋值(通过提取宽度和高度来模拟二维),然后打印出所述向量。这样一个项目的最终目标是在以后拥有一个渲染引擎,该引擎将使用SDL或等效工具将此向量中的int值转换为屏幕上的图像分幅 我以前做过一个程序,可以使用每个包含一个字符的对象向量来做类似的事情,但是我没有通过引用来传递值,所以我真的想在这个程序中通过引用来确定传递值 代码编译得很好,assignToVector函数中的cout语句似乎表明该值被正确分配。但是当我调用最终的print语句时,我想要传递到向量中的值没有正确输出。我正在使用vector.erase在为其赋值之前清除位置,如果有助于缩小问题范围,则使用vector.assign输入值 我真的很感谢大家花时间回答这个问题!谢谢大家! 编辑:下面chris提出的建议修复了问题的第一部分(更改) std::cout两件事:1)在不修改常量迭代器时使用它。2) 使用创建一维向量并通过参考C+赋值+; 我是新手程序员,只需几个月就可以学习C++。这是我关于堆栈溢出的第二个问题,我真的希望这些答案能让我和其他人受益,c++,function,vector,reference,dimensional,C++,Function,Vector,Reference,Dimensional,我开始研究这个程序是因为我想创建一个向量引擎,它可以创建一个一维向量,为该一维向量中的任何位置赋值(通过提取宽度和高度来模拟二维),然后打印出所述向量。这样一个项目的最终目标是在以后拥有一个渲染引擎,该引擎将使用SDL或等效工具将此向量中的int值转换为屏幕上的图像分幅 我以前做过一个程序,可以使用每个包含一个字符的对象向量来做类似的事情,但是我没有通过引用来传递值,所以我真的想在这个程序中通过引用来确定传递值 代码编译得很好,assignToVector函数中的cout语句似乎表明该值被正确分
std::cout Well std::cout
//The purpose of this program is to create a one dimensional vector of integers
//that is equal to the desired screen size. Look at the preprocessor defines
//to change these values.
//The program also contains a function to print the created vector for testing
//purposes.
#include <iostream> //Needed for std::cout and std::endl
#include <vector> //Needed for std::vector and others
#define CONSOLEWIDTH 80;
#define CONSOLEHEIGHT 25;
//This function is supposed to assign a value to a specific point in the 1D vector matrix.
//The posWidth and posHeight are used to compute the location with default console values, mimicking 2D.
void assignToVector(std::vector<int>& intVector, int posWidth, int posHeight, int assignValue);
//This is mostly just a testing function to ensure that the desired
//contents are properly stored in the vector.
void printVector(std::vector<int>& printerVector);
//Creates the test vector, prints it, modifies it, and prints it again
void setupAndRun();
int main()
{
setupAndRun();
}
void assignToVector(std::vector<int>& intVector, int posWidth, int posHeight, int assignValue)
{
int position = posWidth + posHeight*CONSOLEWIDTH;
//std::cout << intVector[position] << std::endl;
std::cout << position << std::endl;
intVector.erase(intVector.begin() + position);
//std::cout << intVector[position] << std::endl;
std::cout << assignValue << std::endl;
intVector.insert(intVector.begin() + position, 1, assignValue);
std::cout << intVector[position] << std::endl;
}
void printVector(std::vector<int>& printerVector)
{
for(std::vector<int>::iterator it = printerVector.begin(); it != printerVector.end(); it++)
{
std::cout << printerVector[*it];
}
}
void setupAndRun()
{
int width = CONSOLEWIDTH;
int height = CONSOLEHEIGHT;
//Creates a vector of size argument1 that each have a value of argument2
//This syntax doesn't seem to work inside classes
std::vector<int> testVector(width*height, 8);
//80*25 8's
printVector(testVector);
//Suppossed to assign 200 to the 10th position of the first row of testVector
assignToVector(testVector, 10, 0, 200);
//Prints out 200
std::cout << testVector[10] << std::endl;
//Prints out default value
std::cout << testVector[9] << std::endl;
//Doesn't have a 200 in it
printVector(testVector);
}