2016-09-14 27 views
1

私は、2つ以上の単語の文字列を受け取り、sleep()メソッドを使用して文字列の各単語を1秒ごとに出力するメソッドをC++で作成しています。私はforループと部分文字列を使ってこれをしようとしています。私は、使用すべき正規表現と、それらをどのように使用して、望ましい出力を達成するべきかについても不明です。個々の単語をC++の文字列から取り出す

私はthisthisを見直しました。これはループでこれを行い、個々の部分文字列を保存しようとしていないので私の質問が異なることがわかります。

入力:

"この"(一時停止) "である"(一時停止) ""(一時停止)」:

所望の出力は、 "これは一例であり、"例。 "

答えて

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++?

+0

優雅な解決策です。 – caps

+0

現在、私の実装は次のようになっています: 'void speak(string statement){ string temp; stringstream ss(ステートメント); while(ss >> temp){ sleep(1); cout << temp << ""; } '私は個々の単語を出す前にこの一時停止をしてもいいですか? –

+0

すごく醜いですね。申し訳ありません。 –

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; 
    } 
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です。

関連する問題