【发布时间】:2021-09-27 07:58:11
【问题描述】:
我正在尝试使用以下代码在 C++ 中打印Deque 的第一项(顶部项):
#include <queue>
#include <deque>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
deque<string> commandHistory;
for (int i = 0; i < 2; i++) {
commandHistory.push_back("asdf");
}
printf(commandHistory.at(1));
return 0;
}
但是,我在 printf 语句中遇到错误:
错误:无法转换 '__gnu_cxx::__alloc_traitsstd::allocator<:__cxx11::basic_string>
,std::__cxx11::basic_string >::value_type’ {aka ‘std::__cxx11::basic_string’} 到 ‘const char*’
但是,我不能像这样将 commandHistory.at(1) 转换为 const char*:
printf((const char*) commandHistory.at(1));
【问题讨论】:
-
std::cout << commandHistory.at(1); -
(std::string 确实提供了访问 char* 值的方法。但在这里不一定合适。请参阅stackoverflow.com/questions/7352099/stdstring-to-char)。
标签: c++ data-structures casting queue