C++ 将输入字符串解析为变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19767644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:08:09  来源:igfitidea点击:

C++ Parsing input string to variables

c++stringparsing

提问by user2939276

All i need to do is to parse the input string of your full name, into 3 separate names so I can take the first initials of each and print them. Can anyone point me in the right direction?

我需要做的就是将您全名的输入字符串解析为 3 个单独的名称,以便我可以获取每个名称的首字母并打印出来。任何人都可以指出我正确的方向吗?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;    
int main()
{
    string [parsed_output] = fullName.split(" ");
    string firstName = parsed_output[0];
    string middleName = parsed_ouput[1];
    string lastName = parsed_output[2];
    string fullName;
    char firstLetter = firstName[0];
    char middleLetter = middleName[0];
    char lastLetter = lastName[0];
    cout << "Enter your first name, middle name or initial, and last name separated by                              spaces: \n"; 
    cin >> fullName;

    cout << "Your initials are: " << firstLetter << middleLetter << lastLetter << '\n';
    cout << "Your name is: " << firstName << " " << middleName << " " << lastName << '\n';
return 0;
}

回答by Some programmer dude

You may want to use e.g. std::getlineto get the full name, and std::istringstreamfor the parsing:

您可能想使用 egstd::getline来获取全名,并std::istringstream进行解析:

std::string fullName;
std::getline(std::cin, fullName);

std::istringstream iss(fullName);

std::string firstName, middleName, lastName;
iss >> firstName >> middleName >> lastName;

回答by Constantin

You're making things way to complicated. What's the meaning of 'parsed_output[0];' and 'fullName' anyway? You could just do:

你让事情变得复杂。'parsed_output[0];' 是什么意思 和“全名”呢?你可以这样做:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;    

int main()
{
  string firstName, middleName, lastName;
  cout << "Enter your first name, middle name or initial, and last name separated by                              spaces: \n"; 
  cin >> firstName >> middleName >> lastName;
  char firstLetter = firstName[0];
  char middleLetter = middleName[0];
  char lastLetter = lastName[0];
  cout << "Your initials are: " << firstLetter << middleLetter << lastLetter << '\n';
  cout << "Your name is: " << firstName << " " << middleName << " " << lastName << '\n';
  return 0;
}

The whole Point of std::cinis, that you get an stream to space separated tokens with the extraction operator '>>'. If you want to extract the first, middle & last names by Hand (like in hasans answer), then you would need the whole string in the first place (a string with all 3 names and whitespaces as limiter). To read in input with containing spaces you would use std::getline(std::cin, fullName)instead of std::cin, because std::cinwill extract the Input until the first trailing whitespace is encountered.

整个要点std::cin是,您可以使用提取运算符“>>”获得一个以空格分隔的标记的流。如果您想手动提取名字、中间名和姓氏(如在 hasans 答案中),那么您首先需要整个字符串(一个包含所有 3 个名字和空格作为限制符的字符串)。要读取包含空格的输入,您将使用std::getline(std::cin, fullName)而不是std::cin, 因为std::cin将提取输入,直到遇到第一个尾随空格。

回答by hasan

string temp = fullname;

string firstName = temp.substr(0, temp.find(" "));

temp = temp.substr(temp.find(" ")+1);
string middleName = temp.substr(0, temp.find(" "));

string lastName = temp.substr(temp.find(" ")+1);