我正在嘗試并行推回 ??3 個向量,當我到達 push_back 字串向量時,出現以下錯誤:
"沒有多載函式實體 "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::string, _Alloc=std::allocatorsstd::string]" 匹配引數 listC/C (304) ask3。 cpp(38, 8):引數型別為:(int) ask3.cpp(38, 8):物件型別為:std::vector<std::string, std::allocatorstd::string> "
這是我所在的代碼塊:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int length, count = 0, moviecount = 0, spacecount = 0;
;
vector<int> status, price;
vector<string> movies;
string FileName, text, line, dummy;
FileName = "MovieList.txt";
ifstream InFile;
InFile.open(FileName);
while (!InFile.eof()) {
getline(InFile, line);
text = line "\n";
}
cout << text;
length = text.length();
for (int i = 0; i <= length; i ) {
if (text[i] == ' ') {
spacecount ;
}
}
if (spacecount == 2) {
moviecount = 1;
}
else if (spacecount > 2) {
int temp = spacecount;
temp = temp - 2;
temp = temp / 3;
moviecount = 1 temp;
}
movies.push_back(moviecount); //<-- problem line
status.push_back(moviecount);
price.push_back(moviecount);
}
uj5u.com熱心網友回復:
movies是 的向量string,所以不能int直接推。
如果您使用的是 C 11 或更高版本,則可以使用std::to_string將整數轉換為字串。
另一種將整數轉換為字串的方法是std::stringstream這樣使用:
std::stringstream ss;
ss << moviecount;
movies.push_back(ss.str());
uj5u.com熱心網友回復:
這是因為您試圖將值推送到初始化為存盤值int的向量中。moviesstring
不知道你想從這段代碼中得到什么,但你可以像這樣將向量的型別更改為 int :或者在 push_back() 之前
vector<int> movies使用將 int 轉換為字串,如下所示:std::to_string()movies.push_back(std::to_string(moviecount))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453464.html
上一篇:黃瓜示例輸入接收不正確
