从C ++中的字符串中取出每个单词

时间:2016-09-14 19:23:14

标签: c++ substring

我正在用C ++编写一个方法,该方法将使用2个或更多单词的字符串,并使用sleep()方法输出字符串的每个单独的单词,大约相隔一秒左右。我试图使用for循环和子串来做到这一点。我也不确定应该使用哪些正则表达式以及如何使用这些正则表达式来实现所需的输出。

我已经审核了thisthis并发现我的问题不同,因为我试图在循环中执行此操作,而不是存储单个子字符串。

输入:

“这是一个例子”

期望的输出:

“此”(暂停)“是”(暂停)“一个”(暂停)“示例。”

3 个答案:

答案 0 :(得分:8)

使用std::stringstream,不需要正则表达式:

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    stringstream ss("This is a test");
    string s;

    while (ss >> s) {
        cout << s << endl;
    }

    return 0;
}

另请参阅How do I tokenize a string in C++?

答案 1 :(得分:2)

以下是一对不涉及创建任何无关缓冲区的实现。

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm/copy.hpp>   //for boost::copy

#include <chrono>
#include <iostream>
#include <string>
#include <experimental/string_view> //in clang or gcc; or use boost::string_ref in boost 1.53 or later; or use boost::iterator_range<char*> in earlier version of boost
#include <thread>

void method_one(std::experimental::string_view sv)
{
    for(auto b = sv.begin(), e = sv.end(), space = std::find(b, e, ' ')
        ; b < e
        ; b = space + 1, space = std::find(space + 1, e, ' '))
    {
        std::copy(b, space, std::ostreambuf_iterator<char>(std::cout));
        std::cout << " (pause) ";   //note that this will spit out an extra pause the last time through
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

void method_two(std::experimental::string_view sv)
{
    boost::copy(
        sv | boost::adaptors::filtered([](const char c) -> bool
        {
            if(c == ' ')
            {
                std::cout << " (pause) ";   //note that this spits out exactly one pause per space character
                std::this_thread::sleep_for(std::chrono::seconds(1));
                return false;
            }

            return true;
        })
        , std::ostreambuf_iterator<char>(std::cout)
    );
}

int main() {
    const std::string s{"This is a string"};

    method_one(s);
    std::cout << std::endl;
    method_two(s);
    std::cout << std::endl;

    return 0;
}

Live on coliru,如果你进入那个。

答案 2 :(得分:1)

你可以实现自己的方法:

    //StrParse.h

    #pragma once

    #include <iostream>

    static counter = 0;

    char* strPar(char* pTxt, char c)
    {

        int lenAll = strlen(pTxt);

        bool strBeg = false;
        int nWords = 0;


        for(int i(0); i < lenAll; i++)
        {
            while(pTxt[i] != c)
            {
                strBeg = true;
                i++;
            }
            if(strBeg)
            {
                nWords++;
                strBeg = false;
            }

        }

        int* pLens = new int[nWords];
        int j = 0;
        int len = 0;

        for(i = 0; i < lenAll; i++)
        {
            while(pTxt[i] != c)
            {
                strBeg = true;
                i++;
                len++;
            }
            if(strBeg)
            {
                pLens[j] = len;
                j++;
                strBeg = false;
                len = 0;
            }

        }

        char** pStr = new char*[nWords + 1];

        for(i = 0; i < nWords; i++)
            pStr[i] = new char[pLens[i] + 1];

        int k = 0, l = 0;

        for(i = 0; i < lenAll; i++)
        {
            while(pTxt[i] != c)
            {
                strBeg = true;
                pStr[k][l] = pTxt[i];
                l++;
                i++;
            }
            if(strBeg)
            {
                pStr[k][l] = '\0';
                k++;
                l = 0;
                strBeg = false;     
            }

        }

        counter++;

        if(counter <= nWords)
            return pStr[counter - 1];
        else
            return NULL;
    }


    //main.cpp


    #include "StrParse.h"

    void main()
    {

        char* pTxt  = "   -CPlusPlus -programming -is -a    -   superb thing ";
        char* pStr1 = NULL;
        int i = 1;
        char sep;

        std::cout << "Separator: ";
        sep = std::cin.get();
        std::cin.sync();

        while(pStr1 = strPar(pTxt, sep))
        {
            std::cout << "String " << i << ": " << pStr1 << std::endl;
            delete pStr1;
            i++;
        }

        std::cout << std::endl;
    }