排序时在空 std::vector 上使用 insert()
Using insert() on empty std::vector when sorting
我知道理想情况下要添加到std::vector
而不必担心我应该使用push_back()
。但是,我的问题是,我需要一个干净的代码来检查我输入的值是否已经在std::vector
中,如果没有,我必须按顺序升序排列。为此,我正在做:
vector<Book>::iterator it;
it = std::find(books.begin(), books.end(), b);
if (it != books.end()) {
*it = b; // if b exists in books, overwrite the iterator
}
else {
vector<Book>::iterator _it;
_it = lower_bound(books.begin(), books.end(), b);
books.insert(_it, b); // here on an empty vector, _it has no values
}
仅当std::vector
中尚不存在值b
时,else
才会运行。如果这是针对它检查的第一个值,则else
运行(因为它为空),并且std::iterator
位于books[0]
(?)。
让我对使用它持谨慎态度的是,在调试时,在insert()
行上,_it
的值显示为"错误读取......"对于std::iterator
指向的每个成员。现在程序功能并产生预期的结果,但它是错误的吗?
你正在做的事情工作正常。然而,这不是最有效的方法。使用std::find
不会利用向量中的数据已排序的事实,它会访问每个元素,直到找到正确的元素。
您可以从一开始就使用std::lower_bound
而不是std::find
因为如果元素存在,它将找到您的元素,如果不存在,它将找到插入新元素的正确位置。
此外,它将使用二叉搜索,因此它将比std::find
更快地突飞猛进。此外,您最终不会两次找到插入/替换点。
像这样的事情应该做:
void insert_or_replace(std::vector<Book>& books, Book const& b)
{
std::vector<Book>::iterator it;
it = std::lower_bound(books.begin(), books.end(), b);
if(it != books.end() && *it == b)
*it = b;
else
books.insert(it, b);
}
这都是定义良好的行为。
lower_bound
将返回空范围的结束迭代器(在您的情况下books.end()
),或者b
是否应该在最后一个元素之后。insert
将在迭代器传递给它之前添加新元素,因此这将在end
之前。 在空向量中,这将具有将元素添加到向量的效果。
代码非常好。对于空向量lower_bound(books.begin(), books.end(), b)
将返回end()
迭代器。将其传递给 std::vector::insert 将正常工作。
pos
- 将在其之前插入内容的迭代器。pos
可能是迭代器end()
关于您的担忧:
让我对使用它持谨慎态度的是,在调试时,在
insert()
行上,_it
的值显示为"读取错误......"对于std::iterator
指向的每个成员。
这是因为end()
迭代器指向最后一个元素后面的位置,因此对它的尊重是无效的(获取不存在的元素)。
- 为什么使用 <uint8_t>MSVC 2015 编译器的 std::vector::insert 比 std::copy 快 5 倍?
- 编译器如何区分"vector::insert"的两种变体?
- STL vector.insert 方法期望_InputIterator作为参数
- 排序时在空 std::vector 上使用 insert()
- map.insert:"Invalid arguments"错误与 pair<enum,vector<*>>
- 如何为我的自定义向量类实现类似 vector.insert 的方法
- Trouble with vector.insert()
- 重载分辨率如何适用于 std::vector:<int>:insert
- 将make_move_iterator与vector::insert一起应用有意义吗?(C++)
- 为什么std::vector::insert需要复制赋值
- C++错误: map.insert(make_pair(struct, vector<struct>));
- std::vector::insert 是否根据定义保留
- 如何使用std::vector.insert()
- std::vector.insert上的Seg故障
- std::move在vector::insert和push_back上的行为不同
- vector::insert在VS2010中执行意想不到的结果
- 为什么std::vector::insert复杂度是线性的(而不是常量)
- 是否允许 vector::insert 只保留一次,避免进一步的容量检查
- c++ vector insert访问违反读取位置
- Std::vector::insert给出意外的结果