简体   繁体   English

在C ++中将字符串读取到结构成员

[英]Reading strings to a structure member in C++

I am from C#, Java background trying to learn C++. 我来自C#,Java背景,试图学习C ++。 The following is a snippet I found for reading values for a structure variable. 以下是我发现的用于读取结构变量值的代码段。 Could not quite figure out what is being done or whether this is the standard way of reading a string structure member. 无法完全弄清正在执行的操作或这是否是读取字符串结构成员的标准方法。 Isn't there a more cleaner way to do this. 没有更干净的方法可以做到这一点。

   string mystr;

   struct structdefn
   {
     string member;
     ...
   } mystruct[10];

   ...
   getline (cin,mystr);
   stringstream(mystr) >> mystruct[n].member;

That code uses a temporary stringstream fed with a line read from stdin to read the data into the struct member. 该代码使用临时stringstream流,该stringstream流中stringstream有从stdin读取的行,以将数据读入struct成员。 Although there are some reasons for which you may be doing that (eg to check that the >> operator consumes the whole line), generally you could more simply do 尽管您可能出于某些原因这样做(例如,检查>>运算符是否占用了整行),但通常您可以更简单地执行

cin >> mystruct[n].member;

As for the explanation of your code: 至于您的代码的解释:

  • getline reads a whole line from cin (the standard input stream) and puts it into the string mystr; getlinecin (标准输入流)中读取整行,并将其放入string mystr中;
  • a temporary object of type stringstream is then created; 然后创建一个类型为stringstream的临时对象; stringstream s are a particular type of streams which aren't fed from a file/socket/whatever, but from a string . stringstream是一种特殊类型的流,它不是从文件/套接字/任何内容中馈送,而是从string馈送。 In this case, the string is mystr , the line just read from the standard input; 在这种情况下,字符串是mystr ,这是从标准输入中读取的行;
  • on that temporary object is called the operator >> , which, in this context, is called the extraction operator 1 ; 在该临时对象上称为运算符>> ,在此上下文中称为提取运算符 1 it's work is to extract from the stream and convert the data that will be put in the variable on its right, in this case mystruct[n].member . 它的工作是从流中提取并转换将放入其右侧变量中的数据,在本例中为mystruct[n].member

The simpler code I posted just uses the operator >> directly from the standard input stream ( cin ), thus avoiding the intermediate passage of the stringstream . 我发布的简单代码只使用标准输入流( cin )中的运算符>> ,从而避免了stringstream流的中间通道。


1. Although with integers it's the binary right shift operator (which it's its original meaning). 1.尽管是整数,但它是二进制右移运算符(这是它的本义)。

Assuming that mystr and mystruct[n].member are both std::string instances, I'm not sure why the stringstream is being used - you should be able to simply read directly into the member string. 假设mystrmystruct[n].member都是std::string实例,我不确定为什么要使用stringstream您应该能够直接读取到member字符串中。

#include <iostream>
#include <string>
// Assuming that mystruct[n].member is a std::string
std::getline ( std::cin, mystruct[n].member );

It would be more helpful to have the declaration of the mystruct structure. 声明mystruct结构会更有用。

EDIT : You can make use of the stringstream if the data you are reading and storing into the structure member is not the entire line of text. 编辑 :如果要读取并存储到结构成员中的数据不是整行文本,则可以使用stringstream The extraction operator >> will extract a "word" at a time from the string, where whitespace delimits the "words" in the line of text. 提取运算符>>将一次从字符串中提取一个“单词”,其中空格分隔文本行中的“单词”。 Example: 例:

// Where the line of text is "123 abc pdq" ...
std::string s;
std::getline ( std::cin, s ); // s now contains: 123 abc pdq
std::stringstream ss ( s );
ss >> mystruct[n].member; // member will contain: 123

There are ways to change the behavior of the stream regarding how the whitespace is handled, but this is the most commonly used way of extracting parts of a string of text into other strings. 有多种方法可以更改有关如何处理空格的流的行为,但这是将文本字符串的一部分提取为其他字符串的最常用方法。

The std::stringstream class is useful for complex string formatting (think printf, but type-safe), and converting between strings and other types. std :: stringstream类对于复杂的字符串格式化(认为printf,但类型安全)以及在字符串和其他类型之间进行转换非常有用。

For example, where you would use Integer.parseInt in Java, you would use std::stringstream in C++. 例如,在Java中使用Integer.parseInt的地方,在C ++中使用std :: stringstream。

int i;
std::stringstream("123") >> i;

If you don't need to do conversion, and you don't need to string formatting, then you probably don't need a stringstream. 如果您不需要进行转换,也不需要字符串格式,那么您可能就不需要字符串流了。

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

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