无法理解为什么我不被允许下标这个载体

kry*_*000 2 c++ vector

我正在尝试运行的代码:

std::string genBlankName(std::vector<Post> &posts)
{
    std::string baseName = "New Post ";
    int postNum = 1;

    for (std::vector<Post>::iterator currentPost = posts.begin(); currentPost != posts.end(); currentPost++)
    {
        if (posts[currentPost].name.substr(0, baseName.length()) == baseName &&
            utils::is_num(posts[currentPost].name.substr(baseName.length(), std::string::npos)) &&
            utils::to_int(posts[currentPost].name.substr(baseName.length(), std::string::npos)) > postNum)
        {
            postNum = utils::to_int(posts[currentPost].name.substr(baseName.length(), std::string::npos));
        }
    }

    return baseName + utils::to_string(postNum);
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

/home/brett/projects/CLPoster/CLPoster-build-desktop/../CLPoster/item.h:240:错误:没有匹配的调用函数 std::vector<cl::Post, std::allocator<cl::Post> >::at(__gnu_cxx::__normal_iterator<cl::Post*, std::vector<cl::Post, std::allocator<cl::Post> > >&)

很抱歉没有多说,但我认为这是一个很常见的事情,我只是不知道自己是一个小组.我会谷歌它,但似乎太普遍的问题,因为我怀疑它更多的是我的实现的问题或类似的东西.

Kon*_*lph 9

订阅需要使用索引,您正在使用迭代器.

您根本不需要下标,只需取消引用迭代器:

currentPost->name.substr(0, baseName.length())
Run Code Online (Sandbox Code Playgroud)

… 等等.


Xeo*_*Xeo 6

您不在下标上使用迭代器.拿一个size_t

for (size_t currentPost = 0; currentPost < posts.size(); ++currentPost)
Run Code Online (Sandbox Code Playgroud)

或者取消引用迭代器:

currentPost->name.substr(0, baseName.length())
Run Code Online (Sandbox Code Playgroud)

  • @kryptobs:你可能想考虑接受帮助你并解决你的问题的答案,甚至可以查看你的旧问题,看看是否有一个好的答案接受. (2认同)