c++ - 使用std::find和std::string数组时出现的问题

标签 c++

我正在尝试运行此比较器:

if (std::find(movements->begin(), movements->end(), commandBuffer) != movements->end())
与:
const std::string movements[8] = {"north", "south", "east", "west", "n", "s", "e", "w"};
其中commandBufferstd::string
在MinGW32 9.2.0上,我得到以下信息:

error: no match for 'operator==' (operand types are 'const char' and 'const std::__cxx11::basic_string')

最佳答案

使用std::cbeginstd::cend,或将数组更改为std::array:

std::array<const std::string, 8> movements = {"north", "south", "east", "west", "n", "s", "e", "w"};
const std::string commandBuffer = "north";

if (std::find(movements.begin(), movements.end(), commandBuffer) != movements.end())
    std::cout << "Found!" << std::endl;

关于c++ - 使用std::find和std::string数组时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64261856/

相关文章:

c++ - Makefile .c.o 当没有 *.cpp 文件时

c++ - 当 Derived 仅添加方法时,将 Base 指针强制转换为 Derived 指针的有效性

带有自定义内存分配器的 std::vector 的 C++ 模板

c++11 std::atomic compare_exchange_weak 和堆栈容器

c++ - 来自 C++ 控制台的大量输入

c++ - 保护 RPC 服务

c++ - 为什么使用 PHP 数百个数据库连接无关紧要..但在 C++ 应用程序中却很重要?

c++ - Win32API : How to determine if EN_CHANGE was because of user action, 不是软件 Action ?

c++ - 运行时链接到不在 LD_LIBRARY_PATH 上的动态库

c++ - 在迭代 std::list 时删除