string find_first does not update string
string find_first does not update string
嘿,我有一个小问题,这个程序工作正常
它接受一个字符串测试,替换所有的"!",并确定"?"answers"。"在哪里,然后将does值放在一个向量上,并只打印字符串中作为问题的部分,如果字符串中没有任何"!",则正常工作,但如果有,则无法识别所有问题。
如果字符串是这样的,它可以工作
std::string test ("ver. what? lol. o que e isto? nao sei. ola? haha. why? adeus. oi! an? haha. lool! ");
但是如果是这样,它就找不到问题。
std::string test ("ver! what? lol! o que e isto? nao sei! ola? haha. why? adeus. oi! an? haha. lool! ");
但是这部分的颂歌应该用"代替所有的"!"。
while (found!=std::string::npos)
{
//std::cout << found << 'n';
test[found]='.';
found=test.find_first_of("!",found+1);
std::cout << test << 'n';
}
Thanks for the help
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
using namespace std;
int main ()
{
std::vector< int > interrogation ;
std::vector< int > dot;
string look = "?";
string look_again = ".";
std::string test ("ver! what? lol! o que e isto? nao sei! ola? haha. why? adeus. oi! an? haha. lool! ");
std::size_t found = test.find_first_of("!");
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
while (found!=std::string::npos)
{
//std::cout << found << 'n';
test[found]='.';
found=test.find_first_of("!",found+1);
std::cout << test << 'n';
}
std::cout << test << 'n';
while (pos != std::string::npos)
{
int a = pos ;
//cout << " . found at : " << sop << std::endl;
interrogation.push_back(a);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
pos = test.find(look, pos + 1);
}
while (sop != std::string::npos)
{
int b = sop;
//cout << " ? found at : " << pos << std::endl;
dot.push_back(b);
string fragment2 = test.substr (0 , sop) ; // works
//cout << fragment2 << endl ;
sop = test.find(look_again, sop + 1);
}
while( interrogation.size() > 0){
std::cout << test << 'n';
while(dot.back() > interrogation.back())
{
cout << "dot.pop_back" << endl;
dot.pop_back();
}
string fragment = test.substr (dot.back() + 1, interrogation.back() - dot.back());
cout << "question with dot n" << fragment << endl ;
interrogation.pop_back() ;
}
}
在用.
(-> while (found!=std::string::npos) { ... }
)替换!
之前,您正在寻找第一个.
位置(-> string::size_type sop = test.find(look_again);
)。在将!
替换为.
的while
循环之后对sop
进行赋值。
如果有人想要它,这里是固定的代码,该代码在分配给名为questions_to_find的文本文件的字符串中查找问题,并将问题保存到名为saved_question的文本文件
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
using namespace std;
int main (){
std::vector< int > interrogation ;
std::vector< int > dot;
string look = "?";
string look_again = ".";
std::string test;
ifstream myfile ("questions_to_find.txt");
while ( getline (myfile,test) )
{
cout << test << 'n';
}
myfile.close();
std::size_t found = test.find_first_of("!");
while (found!=std::string::npos)
{
//std::cout << found << 'n';
test[found]='.';
found=test.find_first_of("!",found+1);
//std::cout << test << 'n';
}
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
while (pos != std::string::npos)
{
int a = pos ;
//cout << " . found at : " << sop << std::endl;
interrogation.push_back(a);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
pos = test.find(look, pos + 1);
}
while (sop != std::string::npos)
{
int b = sop;
//cout << " ? found at : " << pos << std::endl;
dot.push_back(b);
string fragment2 = test.substr (0 , sop) ; // works
//cout << fragment2 << endl ;
sop = test.find(look_again, sop + 1);
}
std::cout << test << 'n';
while( interrogation.size() > 0){
//std::cout << test << 'n';
while(dot.back() > interrogation.back())
{
//cout << "dot.pop_back" << endl;
dot.pop_back();
}
string fragment = test.substr (dot.back() + 1, interrogation.back() - dot.back());
cout << "Question found. n" << fragment << endl ;
// to save text
string save = "saved_question.txt" ;
ofstream myfile;
myfile.open (save.c_str(), ios::app);
myfile << fragment << endl;
myfile.close();
//cout << "Question saved in text file" << endl;
interrogation.pop_back() ;
}
}
如果您的目标是输出字符串中的句子是问题,我认为您在那里做了太多的工作,当您做查找"replace '!' with '。', find '?’,然后在多个循环中输出作为问题的句子。
只执行一个循环从整个字符串中获取问题。
void function(string str) {
string sentence = "";
for(int i=0; i < str.length(); i++) {
if(str[i] == '.' || str[i] == '!')
sentence = ""; // the sentence is not a question so clear the sentence
else if(str[i] == '?') {
sentence.push_back(str[i]);
cout << sentence << endl;
}
else
sentence.push_back(str[i]);
}
}
我不太明白为什么你要在一个向量中存储标点符号(.?)或它的索引(在字符串中)。也许有一个更简单的方法来解析/分割需要显示的字符串?
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 为std::string的某个索引赋值
- 带升压async_read_some "String iterator not dereferncable"
- 编译错误"String does not name a type"
- 我正在接受"Error: string was not declared in this scope"
- q致命参数:将 QString 转换为常量字符* 会导致警告"format string is not a string literal"
- std::string.length() not counting
- "expected unqualified-id before 'void'" & "string in class does not name a type" 错误
- 不断收到错误"member reference base type 'string [1000]' is not a structure or union"不知道如何解决?
- 我在编译时收到此"error: 'string' not declared"
- 命名空间中的"string is not a type"
- 从C++调用 NSLog:"Format string is not a string literal (potentially insecure)"
- String::push_back() does not push back
- C++ "std::string has not been declared"错误
- std::cout of string not working
- Std::string not nothrow move可赋值或可比较
- string find_first does not update string
- std::string == not working?