删除每行括号之间的部分(如果存在)
Removing section between parenthesis on every line if present
我要输入一个文件,其中有一堆看起来像(* blah blah 4324 blah*) 23
的行。我试图删除括号之间的所有内容,并保留右括号后面的数字。
ifstream infile{"nums.txt"};
istream_iterator<string> infile_begin_iter {infile};
istream_iterator<string> eof;
vector<string> cleanNums {infile_begin_iter, eof};
for(int i=0; i<cleanNums.size(); i++){
int openParen=cleanNums[i].find("(*");
int closeParen=cleanNums[i].find("*)");
int distance=openParen-closeParen;
cleanNums[i].erase(closeParen, distance);
}
那段代码一直导致我的程序崩溃。我一直在这里寻找不同的东西,比如getline但我发现它只显示分隔符之前的所有内容
由于没有给出声明,我假设cleanNums
是std::vector<std::string>
。
现在到问题的代码片段:std::string::find()
返回size_type
(即通常是一些整数类型),用于找到给定字符串的位置。因此,openParen
和closeParen
将是找到左圆括号和右圆括号的索引——如果能找到它们的话。当使用size_type
类型的参数调用std::string::erase()时,将这些参数解释为要擦除的部分的起始索引和长度。然而,你叫它好像它在开始索引和最后索引的部分,将被删除。所以你要做的就是在传递给erase()之前,使用这两个索引来计算要删除的部分的长度。
还有另一个问题,可能是导致你的程序崩溃的问题:你没有检查两个std::string::find()
调用是否真的找到了一些东西。因为如果没有,那么它们返回std::string::npos
,这通常比大多数字符串的大小都大。这导致索引超出范围,并且std::string::erase()
抛出std::out_of_range
异常。砰,程序崩溃了!
那么,这里的教训是什么呢?如果你不确定,不要假设函数/方法需要什么样的参数,但是可以在你最喜欢的c++参考中查找。如果编译器不阅读它使用/编译的函数的文档,这是可以的,但是程序员应该阅读他/她至少使用一次的函数的文档。
您可以使用std::getline读取结束的')'
字符,然后您知道下一次读取将是您的号码:
int main()
{
std::ifstream ifs("test.txt");
std::string skip; // read in parts you want to skip
int value; // the value you want to keep
// skip data upto and past ')', then read number
while(std::getline(ifs, skip, ')') >> value)
std::cout << "found: " << value << 'n'; // output just the number
}
- C++擦除(如果存在)
- 如果存在从"双精度"到"T"的转换,则禁用构造函数
- 键入特征以接收 T::value_type(如果存在),否则键入 T
- 我打算调用initializer_list构造函数,如果存在,则事先调用复制构造函数:为什么?
- 如果存在 QSQLITE 数据库,则根据 qt 中的行值插入或更新
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- C++错误:在令牌之前'{'预期','或';'(如果存在)
- 如何将向量一的所有元素与向量二进行比较,如果存在最大元素,则将向量二的所有元素与向量三进行比较?
- 如果存在哈希,请加快C unordered_map插入
- c++ 检查 Unix 中是否存在目录,如果存在,则调用 void 函数
- 调用成员函数(如果存在),回退到自由函数,反之亦然
- 返回成员变量(如果存在)
- 如何将一个数组的每个元素与另一个元素进行比较(如果存在),然后打印是,否则否
- 如果存在任何循环关系,我应该假设弱指针使用吗?
- 如果存在条件,是否有更简单的表示形式
- C 设置值(如果存在成员)
- true/false功能如果存在总和
- 如果存在查询图像,则可以通过数据库中的单个图像获得面部识别
- C++检查文件是否存在,如果存在,则更改输出
- 方法,该方法调用所有基类的同名方法(如果存在),并将返回值保存到列表中