简体   繁体   English

For循环使用getline在vector中输入字符串

[英]For loop to input strings in vector using getline

So I have an assignment for school, I need to declare a vector of strings then use a for loop to input names into the vector using get line. 所以我有一个学校的作业,我需要声明一个字符串向量,然后使用for循环使用get行将名称输入向量中。 This code is what I have so far, I was trying to make a variable for a location in my vector, then input a string into the vector based on the value of my variable. 到目前为止,这段代码是我想要的,我试图为向量中的位置创建一个变量,然后根据变量的值在向量中输入一个字符串。 Im using C++. 我正在使用C ++。

What Im wondering is: whats the flaw in my logic? 我想知道的是:我的逻辑缺陷是什么?

#include "stdafx.h"
#include <iostream>;
#include <vector>;
#include <string>;
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
vector<string> name_list(10);
int n;
for (name_list[n]; cin.getline >> name_list[n]; ++n)
    cin >> name_list[n];
cout << name_list[n];
int stop; cin >> stop;
return 0;
}

EDIT::: So I figured it out! 编辑:::所以我想通了! Thank you 0x499602D2, You kinda set me on the right way. 谢谢0x499602D2,您为我设定了正确的方法。 the code I came up with is this:: 我想出的代码是这样的:

int _tmain(int argc, _TCHAR* argv[])
{
vector<string> name_list(11);
int n = 0;
for (int n = 0; n < 11; ++n);
getline(cin, name_list[n]);
cout << name_list[n];

int stop; cin >> stop;
return 0;
}

Not only is n uninitialized, you're also doing cin.getline >> name_list[n] which shouldn't even compile as far as I know. n不仅是未初始化的,而且您还在做cin.getline >> name_list[n] ,据我所知,它甚至不应编译。 getline is a member function of std::cin that reads a line from input into a character array. getlinestd::cin的成员函数,该函数从输入中读取一行到字符数组。 It's not needed here as we are trying to read into a vector. 由于我们正在尝试读入向量,因此此处不需要它。

Moreover, since you want to get names from the user input into each slot in the vector, attempting to retrieve a line with getline also wouldn't make sense. 此外,由于您希望从用户输入中获取名称,该名称位于向量的每个插槽中,因此尝试使用getline检索也没有任何意义。

n needs to be initialized to an integer that when access with name_list[n] , will give us the start of the vector (that would be 0 ), and instead of getline , we use the operator >> to get each whitespace separated input. n需要初始化为一个整数,当使用name_list[n]访问时,该整数将为我们提供矢量的开头(将为0 ),并且代替getline ,我们使用运算符>>来获取每个空格分隔的输入。 Like this: 像这样:

for (int n = 0; std::cin >> name_list[n]; ++n)
    ; // empty

A statement isn't needed within the for loop body as its already been done in the loop parameters. for循环体内不需要语句,因为它已经在循环参数中完成了。


Another thing you need to look out for is overrunning the size of the vector. 您需要注意的另一件事是超出向量的大小。 You initialized name_list with a size of 10 , and if the user enters in, say, 11 names, accessing an index with name_list[n] will cause Undefined Behavior in your program, which is a special way of saying your program will be invalid. 您初始化了大小为10 name_list ,并且如果用户输入了11名称,则使用name_list[n]访问索引将导致程序中的未定义行为 ,这是一种特殊的说法,表示程序将无效。

It's better to use the at() member function as it will throw an exception if you try to access an out-of-bounds address: 最好使用at()成员函数,因为如果您尝试访问越界地址,它将引发异常

for (int n = 0; std::cin >> name_list.at(n); ++n)
//                                   ^^^^^^
    ;

您需要初始化n = 0,在第二个for参数中,您可能想将cin移入循环并用i <10替换它,因为否则循环将不知道何时停止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM