【发布时间】:2017-07-19 19:07:42
【问题描述】:
我需要将通过 std::cin 传入的单词设置为字符向量,直到到达换行符 ('\n') 字符。 这是我到目前为止所做的:
#include "stdafx.h"
#include <iostream>
#include <vector>
int main(){
std::vector<char> one1; //A char vector that holds the first 'word'
std::cout << "Type in the first set of charactors: " << std::endl;
char o;
std::cin >> o;
int i = 0;
while (std::cin >> o && o != '\n' && o != 0) {
one1[i] = o;
i++;
std::cin >> o;
}
std::cout << "Done";
return 0;
}
一直返回错误,不是编译错误,而是在运行时出现这个错误:
调试断言失败!
程序:C:\WINDOWS\SYSTEM32\MSVCP140D.dll 文件:c:\program files (x86)\microsoft visual studio 14.0\vc\包括\向量 线路:1234
表达式:向量下标超出范围
我不知道出了什么问题,或者是什么导致了这种情况发生,我该怎么办?
【问题讨论】:
-
为什么不读取字符串,然后拆分为字符?