简体   繁体   English

替换向量C ++向量中的元素

[英]replacing elements in a vector of vectors C++

i have a vector of vector that contain just periods (".") so far and I want to replace certain coordinates on the grid with a symbol that I am taking in from an input file. 到目前为止,我有一个向量,该向量仅包含句点(“。”),我想用从输入文件中获取的符号替换网格上的某些坐标。 I am using the replace method but keep getting this error 我正在使用replace方法,但始终收到此错误

" error: no matching function for call to replace(std::basic_string, std::allocator >&, std::basic_string, std::allocator >&, const char [2], const char*)" “错误:没有匹配的函数来调用replace(std :: basic_string,std :: allocator>&,std :: basic_string,std :: allocator>&,const char [2],const char *)”

I am not sure what that error means. 我不确定该错误是什么意思。 I appreciate any and all help. 我感谢所有帮助。 Thanks in advance 提前致谢

here is my code 这是我的代码

 #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;
}

}

As pointed by Chris, the parameters you passed in std::replace are not the correct ones. 克里斯指出,您在std::replace中传递的参数不正确。 std::replace expects iterators for its first two parameters but you are passing references . std::replace的前两个参数需要iterators ,但是您正在传递references

You can use begin() and end() to get the iterators: 您可以使用begin()end()来获得迭代器:
std::replace(grid.at(possRow).begin(), grid.at(possRow).end(), ".", symbol.c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM