在C++中使用字符串::擦除时出错
Error using string::erase in C++
我在编译C++程序时遇到错误。下面是我的代码!
#include <pthread.h>
#include "Path.h"
#include "Maze.h"
#include "SubmitMazeSoln.h"
#include "Assignm3_Utils.h"
#include "Assignm3.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
srand(time(NULL));
string random = "0123";
for (int i = 0; i < 4; i++)
{
int x = (rand () % random.size());
char y = random[x];
random.erase(remove(random.begin(), random.end() + y), random.end());
int temp;
if (threadData.threadIDArrayIndex == 0)
{
temp = i;
}
else
{
temp = y - '0';
}
}
编译程序时的错误。
myprog.cpp: In function ‘void* exploreMaze(void*)’:
myprog.cpp:108:56: error: cannot convert ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
random.erase(remove(random.begin(), random.end() + y), random.end());
对不起,伙计们的帮助深表感谢!谢谢!
正如DaveB所说,
remove(random.begin(), random.end() + y)
应该是
remove(random.begin(), random.end(), y)
错误消息令人困惑,因为random.end() + y
是一个有效的表达式,尽管它生成的迭代器远离容器的末尾。因此,编译器看到对具有两个参数的函数remove
的调用,并尝试理解它。编译器看到一个带有签名remove(const char*)
的函数,并猜测这就是你的意思,然后抱怨它无法将第一个参数转换为类型 const char*
。
如果您使用正确的C++标准库名称(如 std::remove
),则不会发生这种混淆。 using namespace std;
又来了!
相关文章:
- 引用一个已擦除类型(void*)的指针
- 擦除while循环中迭代的元素
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- C++擦除(如果存在)
- 在映射擦除c++期间执行循环的次数
- 为什么擦除方法会影响结束方法
- C++ 字符串类擦除成员函数的时空复杂性
- 类型擦除的std::function与虚拟函数调用的开销
- C++14 中unordered_map矢量和擦除删除成语的奇怪行为
- C++ 擦除函数中需要澄清
- 循环挂起迭代的 std::擦除 on std::list
- 擦除许多矢量元素,同时使用'auto'
- 如何擦除冗余输入?
- C++ STL 设置按值擦除
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 如何在写入文件时擦除最后一个逗号
- 擦除矢量中的对象时出错.如何将移动作业运算符添加到我的类中
- 在C++中使用字符串::擦除时出错
- 插入和擦除后使用矢量迭代器时出错