deque.at无匹配功能

deque.at No Maching Function

我正在尝试从双端队列数据结构中获取双端队列(字符串元素)。但我收到错误:

error: no matching function for call to ‘std::__cxx11::basic_string::basic_string(__gnu_cxx::__alloc_traitsstd::allocator<std::array<std::__cxx11::basic_string<char, 1> >, std::arraystd::__cxx11::basic_string<char, 1> >::value_type&)’ 26 | string record = (string)records.at(0);

deque<array<string, 1>>     records;
string data("hello this is 1st record");
array<string, 1>        buffer{data};
records.push_back(buffer);

string record = (string)records.at(0); //error is reported at this line
printf("%s\n", record.c_str());

有人可以给我提示我做错了什么吗? 作为背景,我必须缓存最后 100 条短信,因此我为此目的使用双端队列。

不太清楚您为什么使用 array 作为元素。 at 返回的值不是字符串而是数组。

deque<array<string, 1>>     records;
string data("hello this is 1st record");
array<string, 1>        buffer{data};
records.push_back(buffer);

string record = records.at(0)[0];
                        ^^ get first element in deque
                              ^^ get first element in array

不要使用 C 风格的转换 ((string)...)。它们几乎总是错误的(如果不是,则应将它们替换为更安全的 C++ 转换)。如果你不使用数组(为什么?当它只包含一个元素时?)代码是

deque<string>     records;
string data("hello this is 1st record");
records.push_back(data);

string record = records.at(0);
                        ^^ get first element in deque