C ++中的拆分字符串[重复]

Posted

技术标签:

【中文标题】C ++中的拆分字符串[重复]【英文标题】:Split string in C++ [duplicate] 【发布时间】:2012-07-14 04:20:50 【问题描述】:

可能重复:How to split a string in C++?C++ split string

如何在 C++ 中从文件中拆分一行(如下所示)?

我想保存具有以下格式的游戏输出结果:

CFC 1 - 0 RES

我有四个变量:

string team1;
string team2;
int goalst1;
int goalst2;

如何拆分字符串,使每个对应部分都在上述四个变量中?

【问题讨论】:

@Drise 如何使用上面提供的格式拆分字符串。团队 1 目标 1 - 目标 2 团队 2。 讽刺的是,你刚刚回答了自己的问题。 为什么人们不提 std::string::find(..) 和 std::string::substr(..)?直截了当的 sln 恕我直言。 【参考方案1】:
string team1;
string team2;
int goalst1;
int goalst2;
string dash;
std::cin >> team1 >> goalst1 >> dash >> goalst2 >> team2;

【讨论】:

输入不是标准输入。是使用getline函数从文件中读取的一行,它存储在一个字符串类型的变量中,我想知道如何拆分字符串以便在变量中有这些值【参考方案2】:

你可以做这样的事情,需要#include <sstream>

char trash;

std::stringstream mystream(myInputString);

mystream >> team1 >> goalst1 >> trash>> goalst2 >> team2;

char trash;

std::stringstream mystream;
mystream << myInputString;

mystream >> team1 >> goalst1 >> trash>> goalst2 >> team2;

编辑:这是更高级但有点整洁。将其粘贴在标题中:

#include <iostream>
#include <string>
#include <array>
#include <cstring>

template<class e, class t, int N>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e(&sliteral)[N]) 
        std::array<e, N-1> buffer; //get buffer
        in >> buffer[0]; //skips whitespace
        if (N>2)
                in.read(&buffer[1], N-2); //read the rest
        if (strncmp(&buffer[0], sliteral, N-1)) //if it failed
                in.setstate(in.rdstate() | std::ios::badbit); //set the state
        return in;

template<class e, class t>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e& cliteral) 
        e buffer;  //get buffer
        in >> buffer; //read data
        if (buffer != cliteral) //if it failed
                in.setstate(in.rdstate() | std::ios::badbit); //set the state
        return in;

template<class e, class t, int N>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, e(&carray)[N]) 
        return std::operator>>(in, carray);

template<class e, class t, class a>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, a& obj) 
        return in >> obj; //read data

这允许您在流中执行字符串文字:

std::stringstream mystream(myInputString);

mystream >> team1 >> goalst1 >> '-' >> goalst2 >> team2;

std::stringstream mystream;
mystream << myInputString;

mystream >> team1 >> goalst1 >> '-' >> goalst2 >> team2;

另请参阅:Safer but easy-to-use and flexible C++ alternative to sscanf()

【讨论】:

输入不是标准输入。是使用getline函数从文件中读取的一行,它存储在一个字符串类型的变量中,我想知道如何拆分字符串以便在变量中有这些值【参考方案3】:

如果我猜对了,您可能正在尝试从文件中读取。然后使用 ifstream,您可以像从 cin 这样的标准输入中读取文件一样读取文件。

  ifstream myfile("filename");

现在使用 myfile 代替 cin 运算符,你就完成了..

【讨论】:

【参考方案4】:

我喜欢 boost::split 从事这样的工作:

#include <string>
#include <vector>

#include <boost/algorithm/string/split.hpp>

struct Result 
     std::string team1;
     std::string team2;
     unsigned goals1;
     unsigned goals2;
;

Result split(std::string const& s) 
    std::vector<std::string> splitted;
    boost::split(s, splitted, boost::token_compress_on);

    if (splitted.at(2) != "-") 
        throw runtime_error("The string format does not match");
    

    Result r;
    r.team1 = splitted.at(0);
    r.team2 = splitted.at(4);
    r.goals1 = stoi(splitted.at(1));
    r.goals2 = stoi(splitted.at(3));

    return r;

【讨论】:

以上是关于C ++中的拆分字符串[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在数据框中的分隔符处拆分列[重复]

Leetcode No.140 单词拆分 II(DFS)

Leetcode No.140 单词拆分 II(DFS)

在 Python 中声明编码 [重复]

C语言 输入一个字符串 去掉重复的字符,输出无重复的

C语言,删除数组中的重复数字然后输出