如何访问c ++中的某些元素 [英] how to access certain elements in c++

查看:85
本文介绍了如何访问c ++中的某些元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我正在尝试创建一个询问用户个人问题的程序。



I am new to C++ and am trying to create a program that asks the user personal questions.

std::vector<std::string> name, age, favsinger;
std::cout << "Hello, what is your Name Age Favorite_Singer? ";
std::cin << name;    //i want to store this into a list along with sibling's name/age/favsinger as well
std::cin << age;
std::cin << favsinger;

int n;
std::cout << "How many siblings do you have? ";
std::cin << n;

for (int a=0;a<n;a++){>
    std::cout << "Please enter Name Age Favorite_Singer for sibling #" << a << ": ";
    std::string a, b, c;
    std::cin >> a;
    std::cin >> b;
    std::cin >> c; //this part throws me off because favorite singer has a first_name SPACE last_name
    name.push_back(a);
    age.push_back(b);
    favsinger.push_back(c);

}





假设用户输入了3兄弟姐妹的信息:



Michael 24 Madonna

Sam 20 Michael Jackson

Anna 18无



i希望能够访问兄弟姐妹的每个偏好,无论是他们的姓名,年龄还是最喜欢的歌手。



我尝试过做



年龄[1]



但如果我这样做

< br $>
favsinger [1]



由于空间而被抛弃。



Let's say that the user inputted "3" siblings with the information:

Michael 24 Madonna
Sam 20 Michael Jackson
Anna 18 None

i want to be able to access each of the sibling's preferences, whether it be their name, age, or favorite singer.

I tried doing

age[1]

but if i do

favsinger[1]

it gets thrown off because of the space.

推荐答案

您的主要问题是您不清楚单个值的区别。对于string类型的变量,运算符>> 的标准行为是它在下一个空格字符之前读取任何内容。当然这可能会导致问题,如果你的价值观本身可能包含空格。



如果有人输入Lisa Marie 55 10 cc你会遇到更严重的问题

(参见 http://en.wikipedia.org/wiki/10cc [< a href =http://en.wikipedia.org/wiki/10cctarget =_ blanktitle =New Window> ^ ])

现在,哪一部分是这个名字,数字(年龄),以及哪个歌手/乐队?



你可以要求cin跳过空格,e。 G。通过使用操纵器 std :: skipws(),但如果这样做,它将读取整行。为此更容易使用 std :: getline()(参见http://www.cplusplus.com/reference/string/string/getline/ [ ^ ])



当然,之后你需要将线分成单独的字段,你回到原来的问题:一个字段在哪里结束,下一个开始?



最简单的解决方案:每行输入一个字段!



比较棘手的一个:使用特殊的分隔符,e。 G。 ,。您需要使用该分隔符对您的行进行标记。我建议反对它:它更难实现并依赖于正确的用户输入。
Your main problem is that you aren't clear on what separates the individual values. The standard behaviour of operator >> for variables of type string is that it reads anything until the next whitespace character. Of course this can lead to problems, if your values may contain whitespace themselves.

You would run into even more serious problems if someone typed in "Lisa Marie 55 10 cc"
(see http://en.wikipedia.org/wiki/10cc[^] )
Now, which part is the name, which the number (age), and which the singer / band?

You could ask cin to skip whitespace, e. g. by using the manipulator std::skipws(), but if you do that, it would read the entire line. It's easier to just use std::getline() for that (see http://www.cplusplus.com/reference/string/string/getline/[^] )

Of course, after that you need to split up the line into individual fields, and you're back to the original problem: where does one field end and the next start?

The easiest solution: just enter one field per line!

The trickier one: use a special separator, e. g. ','. You'd need to tokenize your line using that separator then. I'd advise against it though: it's harder to implement and dependend on correct user input.


这篇关于如何访问c ++中的某些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆