我正在嘗試從 deque 資料結構中 deque(一個字串元素)。但我得到和錯誤:
錯誤:沒有匹配的函式呼叫 '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 | 字串記錄=(字串)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 條短信,因此我為此使用了 deque。
uj5u.com熱心網友回復:
不太清楚您為什么使用arrayas 元素。回傳的值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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/380352.html
