引用改变大小的向量时出现分段错误
因此,我在主函数中初始化一个向量,并将该向量作为引用传递到其大小发生变化的函数中。现在,我想象主函数中初始化的向量的大小会改变,但事实并非如此。我只是错误地执行了此操作吗?
#include <iostream>
#include <vector>
using std::vector;
void createImage(vector<vector<char> >& img){
int cols, rows;
std::cin >> rows >> cols;
img.clear();
for(int i = 0; i < rows; i++)
img.push_back(vector<char>(cols, 'O'));
}
void colorPixel(vector<vector<char> > &img){
int xCoord, yCoord;
char color;
std::cin >> xCoord >> yCoord >> color;
img[xCoord][yCoord] = color;
}
void printVector(vector<vector<char> > &img){
for(int i = 0; i < img.size(); i++){
for(int j = 0; j < img[i].size(); j++)
std::cout << img[i][j];
std::cout << '\n';
}
}
int main(){
char operation;
while(std::cin >> operation){
vector<vector<char> > myVector;
if(operation == 'X')
break;
if(operation == 'I')
createImage(myVector);
if(operation == 'C')
printVector(myVector);
if(operation == 'L')
colorPixel(myVector);
}
}
编辑: 这就是正在发生的事情。当您创建向量时, I 5 5 调用 createImage() ,这是可行的,但当您 L 4 4 P 调用 myVector[4][4] 处的 colorPixel() 时,它开始失败。
如果我将 main 中的 myVector 初始化为 250 250,则 print 将打印所有 250 行,即使在 createImage() 中更改了大小(例如 4 4)之后也是如此。
简而言之,我相信并且确信,main 中的向量没有得到其大小已更改的提示,是否有任何解决方案?
So I initialize a vector within a main function, and pass that vector as a reference into a function where its size changes. Now, I would imagine that the initialized vector within the main would change in size, but its not. Am I just implementing this incorrectly?
#include <iostream>
#include <vector>
using std::vector;
void createImage(vector<vector<char> >& img){
int cols, rows;
std::cin >> rows >> cols;
img.clear();
for(int i = 0; i < rows; i++)
img.push_back(vector<char>(cols, 'O'));
}
void colorPixel(vector<vector<char> > &img){
int xCoord, yCoord;
char color;
std::cin >> xCoord >> yCoord >> color;
img[xCoord][yCoord] = color;
}
void printVector(vector<vector<char> > &img){
for(int i = 0; i < img.size(); i++){
for(int j = 0; j < img[i].size(); j++)
std::cout << img[i][j];
std::cout << '\n';
}
}
int main(){
char operation;
while(std::cin >> operation){
vector<vector<char> > myVector;
if(operation == 'X')
break;
if(operation == 'I')
createImage(myVector);
if(operation == 'C')
printVector(myVector);
if(operation == 'L')
colorPixel(myVector);
}
}
Edit:
This is what is effectively happening. When you create the vector so I 5 5 which calls createImage(), that works, but it begins to fail when you L 4 4 P which calls colorPixel() at myVector[4][4].
If I were to initialize myVector in main to 250 250, then print will print all 250 lines, even after being changed in size (such as 4 4) in createImage().
In short, I believe, and know for sure, that the vector within main is not getting the hint that its size has changed, is there any fix for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此
while
循环中发生的第一件事是创建一个新的myVector
。从字面上看,这就是 C++ 中的内容。当到达循环末尾时,此
myVector
将被销毁。这就是 C++ 的工作原理:如果您在循环中或在 if 语句内声明一个对象,则当执行到达此范围的末尾时,该对象将被销毁。因此,当这个 while 循环到达终点时,这个 myVector 就会被销毁。它不再存在了。它将会消失。它将轻松存在。它将渴望峡湾。它将成为前 myVector。
然后,再次检查
while
循环的条件,如果条件仍然成立,则执行再次进入while
,创建一个新的myVector
,从头开始。无论如何,它与之前的 myVector 没有任何关系,后者只是一个微弱的记忆。解决这个问题的方法是在
main
的开头声明这个myVector
之前这个循环,因此,只有当main
本身完成执行时,这个myVector
才会被销毁。The very first thing that happens in this
while
loop is that a newmyVector
gets created. This is, literally, what this says in C++.When the end of the loop gets reached, this
myVector
gets destroyed. That's how C++ works: if you declare an object in a loop, or inside theif
statement, the object gets destroyed when execution reaches the end of this scope.So, when this
while
loop reaches this end, thismyVector
gets destroyed. It becomes no more. It will will be gone. It will ease to exist. It will be pining for the fjords. It will become an ex-myVector.Then, the
while
loop's condition gets checked again, and if the condition still holds true, execution enters thewhile
again, creating a newmyVector
, from scratch. It will have nothing to do, whatsoever, with the previous myVector, which is just a faint memory.And the way to fix this is to declare this
myVector
before this loop, at the beginning of yourmain
, so thismyVector
gets destroyed only whenmain
, itself, finishes executing.