从C+中的字符串中提取每个单词+; 我在C++中编写一个方法,它将使用2个或更多个单词的字符串,并输出每个字符串的一个单独的单词,用一个第二个左右的分隔符,使用睡眠()/方法。我正在尝试使用for循环和子字符串来完成这一点。我也不确定应该使用哪些正则表达式,以及如何使用它们来实现所需的输出
我已经复习了一遍,发现我的问题有所不同,因为我试图在循环中执行此操作,而不是存储单个子字符串 输入: “这是一个例子” 所需输出:从C+中的字符串中提取每个单词+; 我在C++中编写一个方法,它将使用2个或更多个单词的字符串,并输出每个字符串的一个单独的单词,用一个第二个左右的分隔符,使用睡眠()/方法。我正在尝试使用for循环和子字符串来完成这一点。我也不确定应该使用哪些正则表达式,以及如何使用它们来实现所需的输出,c++,substring,C++,Substring,我已经复习了一遍,发现我的问题有所不同,因为我试图在循环中执行此操作,而不是存储单个子字符串 输入: “这是一个例子” 所需输出: “This”(pause)“是”(pause)“一个”(pause)“示例。”使用std::stringstream,不需要正则表达式: #include <iostream> #include <sstream> using namespace std; int main() { stringstream ss("This is
“This”(pause)“是”(pause)“一个”(pause)“示例。”使用
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;
}
#包括
#包括
使用名称空间std;
int main(){
stringstream ss(“这是一项测试”);
字符串s;
while(ss>>s){
不能使用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;
}
#包括
#包括
使用名称空间std;
int main(){
stringstream ss(“这是一项测试”);
字符串s;
while(ss>>s){
cout您可以实现自己的方法:
//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;
}
//StrParse.h
#布拉格语一次
#包括
静态计数器=0;
char*strPar(char*pTxt,char c)
{
int lenAll=strlen(pTxt);
bool-strBeg=假;
int nWords=0;
for(int i(0);i 如果(计数器您可以实现自己的方法:
//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;
}
//StrParse.h
#布拉格语一次
#包括
静态计数器=0;
char*strPar(char*pTxt,char c)
{
int lenAll=strlen(pTxt);
bool-strBeg=假;
int nWords=0;
对于(int i(0);i if(counter这里有一对不涉及创建任何无关缓冲区的实现
#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;
}
#包括
#include//for boost::copy
#包括
#包括
#包括
#在clang或gcc中包含//;或者在boost 1.53或更高版本中使用boost::string\u ref;或者在boost的早期版本中使用boost::iterator\u range
#包括
无效方法(标准::实验::字符串视图sv)
{
对于(auto b=sv.begin(),e=sv.end(),space=std::find(b,e.))
;b std::cout这里有两个实现,它们不涉及创建任何无关的缓冲区
#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;
}
#包括
#include//for boost::copy
#包括
#包括
#包括
#在clang或gcc中包含//;或者在boost 1.53或更高版本中使用boost::string\u ref;或者在boost的早期版本中使用boost::iterator\u range
#包括
无效方法(标准::实验::字符串视图sv)
{
对于(auto b=sv.begin(),e=sv.end(),space=std::find(b,e.))
;b std::cout是一个优雅的解决方案。到目前为止,我的实现看起来如下:void speak(string语句){string temp;stringstream ss(语句);while(ss>>temp){sleep(1);cout@A.Takami这正是您的实现所做的;您只需要刷新输出以确保它是正确的