从向量获取值
obtaining values from a vector
我的代码在输入字符串后出现错误。 我需要有一个字符串输入(例如添加水果),它将通过"add"关键字放入我的向量中。但是当我也输入一个单词(例如打印)时,打印我的矢量中的元素。它行不通。有什么见解吗?
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <string> separate(string str){
string word = "";
vector <string> v = {0,0};
for (auto x : str)
{
if (x == ' ')
{
v[1] = word;
word = "";
}
else
{
word = word + x;
}
}
v[0] = word;
return v;
}
int main(){
string user_input, command, item;
int cmds;
vector <string> result;
while (1){
cout << "Enter a command: ";
getline(cin, user_input);
vector <string> arrcmd = separate(user_input);
(arrcmd.size() == 1) ? (command = arrcmd[0]) : (command = arrcmd[1], item = arrcmd[0]);
cout << arrcmd.size() << endl;
/* command = arrcmd[0], item = arrcmd[1]; */
cout << command << " " << item << endl;
vector <string> v = {0,0};
创建一个包含两个元素的向量。因此,separate
总是返回大小为 2 的向量 - 它并不总是填充其第二个元素。
main
尝试根据向量大小做出决策 - 但从未采用size() == 1
路径。
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 从 exprTk 获取向量作为输出
- C++ 获取向量中常量字符* [ ] 的长度
- 如何使用模板获取向量中的数据类型?
- C++ 通过 std::transform 的 vector 索引获取向量
- C++ 运算符"="重载 - 在 lhs 上获取向量中的所有值,使其等于 rhs 上的双精度值
- 通过交互器获取向量中的索引
- 无法获取向量中元素的总和
- 获取向量中元素的地址
- 如何获取向量的每个第 N 个元素的"address"
- 我应该如何从特征 3 中的张量切片中获取向量
- 如何获取向量的排序索引
- 向量C ,获取向量的第二个元素有什么问题
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 获取向量的指定元素
- 如何在具有相同基类的两个不同类中获取向量
- 如何获取向量的这些元素,其中另一个向量在相应的索引处具有'1
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 获取向量 c++ 中的字符串计数
- C++linux获取向量迭代器不兼容