C++ 如何查看boost集合中的下一个和上一个元素<;cpp_int>;

C++ 如何查看boost集合中的下一个和上一个元素<;cpp_int>;,c++,boost,C++,Boost,我试图将boost整数cpp_int存储在一个有序的集合中,并使用以下代码检查下一个和上一个元素: #include <boost/multiprecision/cpp_int.hpp> #include <boost/unordered_set.hpp> #include <iostream> namespace mp = boost::multiprecision; using boost::unordered_set; using namespace

我试图将boost整数cpp_int存储在一个有序的集合中,并使用以下代码检查下一个和上一个元素:

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/unordered_set.hpp>
#include <iostream>

namespace mp = boost::multiprecision;
using boost::unordered_set;
using namespace std;

int main() {

        set<mp::cpp_int> st;
        set<mp::cpp_int>::iterator it, it1, it2;
        //pair<set<mp::cpp_int>::iterator,bool> res;
        boost::tuples::tuple<set<mp::cpp_int>::iterator, bool> tp;

        int i = 0, temp;
        while(i<10){

            cin>>temp;

            tp = st.insert(temp);
            it = get<0>(tp);
            it1 = prev(it);
            it2 = next(it);
            cout<<*it1<<endl;
            //cout<<*it2<<endl;

            i++;
        }

    return 0; 
}

使用boost时,使用set和迭代器的正确方法是什么?

在取消引用
it1
it2
之前,您需要检查是否存在上一个/下一个元素,例如:

std::set<mp::cpp_int> s;

for (size_t i = 0; i < 10; ++i){

    std::cin >> temp;

    auto p = s.insert(temp);
    if (p.second) { // insertion succeed

        auto it = p.first;

        std::cout << "Inserted: " << *it << '\n';

        if (it != s.begin()) { // not the first, there is a previous element
            auto it1 = std::prev(it);
            std::cout << "Previous: " << *it1 << '\n';
        }
        else {
            std::cout << "Previous: None\n";
        }

        auto it2 = std::next(it);
        if (it2 != s.end()) { // there is a next element
            std::cout << "Next: " << *it2 << '\n';
        }
        else {
            std::cout << "Next: None\n";
        }
    }

}

在取消引用
it1
it2
之前,您需要检查是否存在上一个/下一个元素,例如:

std::set<mp::cpp_int> s;

for (size_t i = 0; i < 10; ++i){

    std::cin >> temp;

    auto p = s.insert(temp);
    if (p.second) { // insertion succeed

        auto it = p.first;

        std::cout << "Inserted: " << *it << '\n';

        if (it != s.begin()) { // not the first, there is a previous element
            auto it1 = std::prev(it);
            std::cout << "Previous: " << *it1 << '\n';
        }
        else {
            std::cout << "Previous: None\n";
        }

        auto it2 = std::next(it);
        if (it2 != s.end()) { // there is a next element
            std::cout << "Next: " << *it2 << '\n';
        }
        else {
            std::cout << "Next: None\n";
        }
    }

}

在取消引用之前,需要检查
it1
it2
是否有效。在获取it1和it2之前,我正在插入一个元素。所以,我应该总是有一个有效的迭代器。
应该总是有效的,但是
it1
it2
不能保证有效-在一个元素集中,第一个元素的前一个元素是什么?在取消引用它们之前,需要检查
it1
it2
是否有效。在获取it1和it2之前,我正在插入一个元素。所以,我应该总是有一个有效的迭代器。
它应该总是有效的,但是
it1
it2
不能保证是有效的-在一个只有一个元素的集合中,第一个元素的前一个元素是什么?我想我弄错了。谢谢但C++引用()表示,如果我尝试插入和元素,如果插入失败,它将给我现有元素的迭代器。所以我试着使用它。@user3243499是的,但如果有人在事后阅读您的代码,那么这显然会产生误导,加上
insert
将插入一个新元素,而
std::find
不会修改
set
,这可能是您在查找上一个/下一个时想要的。我想我弄错了。谢谢但C++引用()表示,如果我尝试插入和元素,如果插入失败,它将给我现有元素的迭代器。所以我试着使用它。@user3243499是的,但如果有人在事后阅读您的代码,那么这显然会产生误导,加上
insert
将插入一个新元素,而
std::find
不会修改
set
,这可能是您在查找上一个/下一个时想要的。
 auto it = s.find(temp);
 if (it != s.end()) {
     // Same code as above.
 }