【问题标题】:Rearranged vector elements after std::unique在 std::unique 之后重新排列向量元素
【发布时间】:2021-11-16 23:54:58
【问题描述】:

我目前正在阅读 Stanley Lippman 的 C++ Primer。第 10 章介绍了泛型算法。

例如std::sortstd::uniquestd::vector 成员函数erase 应用于删除向量中的重复元素。

要查看 std::unique 如何重新排列向量元素,我尝试打印每个元素,只是发现并非所有元素都被打印。然而,调用.size() 表明向量的大小与预期的一样没有变化。

编译程序后:

clang++ -std=c++11 -o elimDubs elimDubs.cc

并用

调用程序
./elimDubs the quick red fox jumps over the slow red turtle

程序打印

Size after std::unique: 10
fox jumps over quick red slow the turtle the  

这只是 10 个元素中的 9 个。 (red 不见了) 为什么?对于程序来说,这并不重要,因为随后调用erase 无论如何都用于删除重复的元素,但仍然让我恼火的是缺少元素或至少没有打印出来。

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

void elimDubs( std::vector<std::string> &words )
{
  std::sort( words.begin(), words.end() );

  auto end_unique = std::unique( words.begin(), words.end() );


  std::cout << "Size after std::unique: "
            << words.size() << std::endl;

  for ( const auto &el : words )
    std::cout << el << " ";
  std::cout << std::endl;
}



int main(int argc, char **argv)
{
  std::vector<std::string> sentence;

  if ( argc < 2 )
    return -1;

  std::copy( argv + 1, argv + argc,
             std::back_inserter(sentence) );

  elimDubs( sentence );
}

【问题讨论】:

  • 这并没有解决问题,但是sentence可以直接用std::vector&lt;std::string&gt; sentence(argv + 1, argv + argc);初始化。

标签: c++ c++11 vector stl


【解决方案1】:

std::unique 是一个破坏性的过程。引用cppreference

删除是通过移动范围内的元素以覆盖要删除的元素来完成的。

这意味着std::unique 返回的新结束迭代器之后的任何元素都将处于有效但未指定的状态。它们不应该被访问,因为它们应该通过调用 erase 从向量中删除。

注释部分也注明了这一点:

[r, last)(如果有)中的迭代器,其中r 是返回值,仍然是可取消引用的,但元素本身具有未指定的值。调用unique 之后通常会调用容器的erase 成员函数,该函数会删除未指定的值并减小容器的物理大小以匹配其新的逻辑大小。

【讨论】:

【解决方案2】:

还有 10 个元素;只是其中一个被“移出”。如果您更改打印循环以引用单词,则:

  for ( const auto &el : words )
    std::cout << "'" << el << "'" << " ";

您将看到以下输出:

'fox' 'jumps' 'over' 'quick' 'red' 'slow' 'the' 'turtle' 'the' ''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2021-06-29
    • 2019-06-24
    • 1970-01-01
    • 2019-04-23
    • 2013-09-17
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多