Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 列表的越界迭代器_C++_Visual C++ - Fatal编程技术网

C++ 列表的越界迭代器

C++ 列表的越界迭代器,c++,visual-c++,C++,Visual C++,执行以下操作是否有效或未定义: int main() { std::list<int> a = { 10 }; auto it = std::prev(a.begin()); // potential out of bounds here std::list<int> b; b.splice(b.begin(), a); a.push_back(12); std::cout << *std::next(

执行以下操作是否有效或未定义:

int main()
{
    std::list<int> a = { 10 };

    auto it = std::prev(a.begin()); // potential out of bounds here

    std::list<int> b;

    b.splice(b.begin(), a);

    a.push_back(12);

    std::cout << *std::next(it);

    return 0;
}
intmain()
{
std::列表a={10};
auto it=std::prev(a.begin());//此处可能超出范围
std::列表b;
b、 拼接(b.begin(),a);
a、 推回(12);

std::可能是UB,但如果我正确理解了你的意思,为什么不反转代码的工作方式并引用下一个对象呢?你可以同样轻松地返回到上一个对象,并在允许的最后一个元素之后再返回一个(你得到
end()
)。这是一个很好的建议,但我需要它同时工作。你说“双向”是什么意思?
list.erase()
返回一个迭代器,该迭代器指向它的基本删除位置,
list.insert()
将一个项放在该迭代器上。无需重新实现此迭代器保存机制。我想您可能必须手动将第一个元素保留为“哨兵”,而不在其中存储任何数据。
// Note not valid code... really just a concept of what I need...
std::list<int> a = { 10, 20, 30, 40 };
std::list<int> b = { 2, 4, 6, 8 };

auto b1 = std::advance(a.begin(), 1);
auto e1 = std::advance(a.end(),  -2);

auto b2 = b.begin();
auto e2 = b.end();

a.splice(b1, e1, b, b2, e2);

// a = { 10, 2, 4, 6, 8, 40 }
// b = { 20, 30 }

b.splice(b1, e1, a, b2, e2);

// a = { 10, 20, 30, 40 }
// b = { 2, 4, 6, 8 };