C++ 替换向量C+的向量中的元素+;
我有一个只包含句点的向量向量 (“”)到目前为止,我想用从输入文件中获取的符号替换网格上的某些坐标。我正在使用替换方法,但不断出现此错误 错误:没有用于替换调用的匹配函数(std::basic_string,std::allocator>&,std::basic_string,std::allocator>&,const char[2],const char*) 我不知道那个错误是什么意思。我感谢所有的帮助。提前谢谢 这是我的密码C++ 替换向量C+的向量中的元素+;,c++,string,vector,C++,String,Vector,我有一个只包含句点的向量向量 (“”)到目前为止,我想用从输入文件中获取的符号替换网格上的某些坐标。我正在使用替换方法,但不断出现此错误 错误:没有用于替换调用的匹配函数(std::basic_string,std::allocator>&,std::basic_string,std::allocator>&,const char[2],const char*) 我不知道那个错误是什么意思。我感谢所有的帮助。提前谢谢 这是我的密码 #include <vector> #inclu
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string locationfilename, filenames,symbol;
int numRows, numCols, startRow, startCol, endRow, endCol, possRow, possCol, id;
cout<< "Enter Locations Filename"<<endl;
cin>>locationfilename;
cout<< "Enter Names Filename"<<endl;
cin>>filenames;
ifstream locations(locationfilename.c_str());
ifstream names(filenames.c_str());
locations>>numRows>>numCols>>startRow>>startCol>>endRow>>endCol;
vector <string> rows(numCols,".");
vector< vector<string> > grid(numRows,rows);
locations>>possRow>>possCol>>symbol>>id;
while(!locations.fail())
{
if(possRow>numRows || possCol>numCols)
{
cout<<id<< " out of bounds-ignoring"<<endl;
}
else
{
replace(grid.at(possRow).front(),grid.at(possRow).back(),".",symbol.c_str());
}
locations>>possRow>>possCol>>symbol>>id;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串位置文件名、文件名、符号;
int numrow、numCols、startRow、startCol、endRow、endCol、possRow、possCol、id;
coutnumRows>>numCols>>startRow>>startCol>>endRow>>endCol;
向量行(numCols,“.”);
矢量<矢量>栅格(numRows,行);
位置>>possRow>>possCol>>符号>>id;
而(!locations.fail())
{
if(possRow>numRows | | possCol>numCols)
{
cout>符号>>标识;
}
}
正如Chris所指出的,您在std::replace
中传递的参数是不正确的。std::replace
要求前两个参数使用迭代器,但您传递的是引用
您可以使用begin()
和end()
获取迭代器:
std::replace(grid.at(possRow).begin(),grid.at(possRow).end(),”,symbol.c_str());
std::replace
使用迭代器。