小编use*_*038的帖子

将std :: lower_bound与std :: vector :: const_iterator一起使用

我试图在向量的一部分中找到从下一个迭代器位置到向量末尾的边界.

代码是:

#include <algorithm>
#include <vector>
#include <iostream>

int
main()
{
    typedef std::vector<int> Vector;

    Vector v;
    v.push_back(3);
    v.push_back(7);
    v.push_back(15);
    v.push_back(21);
    std::sort(v.begin(), v.end());
    for (Vector::const_iterator i = v.begin(); i != v.end(); ++i) {
        Vector::const_iterator low = std::lower_bound(i+1, v.end(), -10-*i);
        Vector::const_iterator high = std::upper_bound(i+1, v.end(), 10-*i);
        for (Vector::const_iterator j = low; j != high; ++j) {
            std::cout << *i << "~" << *j << std::endl;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,std::lower_bound(i+1, v.end(), -10-*i)触发编译错误,我无法理解:

c++ -O3 -ggdb -std=c++11 -W -Wall -pedantic …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector c++11

1
推荐指数
1
解决办法
1369
查看次数

标签 统计

c++ ×1

c++11 ×1

stdvector ×1