2

根据 cplusplus.comstd::set::insert()采用提示迭代器插入项目的重载从 C++98 更改为 C++11。在 C++98 中,提示应该是:

如果位置指向将在插入元素之前的元素,则该函数优化其插入时间。

但是,在 C++11 中,提示已更改,现在应该是:

如果位置指向将跟随插入元素的元素(或者指向末尾,如果它是最后一个),则该函数优化其插入时间。

但是,在 C++98 或 C++11 中,返回值是相同的:

一个迭代器,指向新插入的元素或集合中已经具有相同值的元素。

对于 C++98,我有插入一系列相邻项的代码,如下所示:

void example98(std::set &_sent, int beginOffset, int lastOffset) {
  std::set<int>::iterator itr = _sent.end();

  for (int offset = beginOffset; offset <= lastOffset; ++offset) {
    itr = _sent.insert(itr, offset);
  }
}

我可以在 C++11 中将其更改为:

void example11(std::set &_sent, int beginOffset, int lastOffset) {
  std::set<int>::iterator itr = _sent.end();

  for (int offset = lastOffset; offset >= beginOffset; --offset) {
    itr = _sent.insert(itr, offset);
  }
}

但是我需要重构代码以从 C++98 到 C++11,我是否在这里做错了什么,或者如果没有,这种改变的动机是什么,为什么insert()改变的论点但是不是返回值?

4

0 回答 0