C++ std::copy用法

转载自:http://blog.sina.com.cn/s/blog_4cd2484f010099vt.html

如果要把一个序列(sequence)拷贝到一个容器(container)中去,通常用std::copy算法,代码如下:

std::copy(start, end, std::back_inserter(container));

 这里,start和end是输入序列(假设有N各元素)的迭代器(iterator),container是一个容器,该容器的接口包含函数push_back。假设container开始是空的,那么copy完毕后它就包含N个元素,并且顺序与原来队列中的元素顺序一样。标准库提供的back_inserter模板函数很方便,因为它为container返回一个back_insert_iterator迭代器,这样,复制的元素都被追加到container的末尾了。 

现在假设container开始非空(例如:container必须在循环中反复被使用好几次)。那么,要达到原来的目标,必须先调用clear函数然后才能插入新序列。这会导致旧的元素对象被析构,新添加进来的被构造。不仅如此,container自身使用的动态内存也会被释放然后又创建,就像list,map,set的节点。某些vector的实现在调用clear的时候甚至会释放所有内存。

 

通常,考虑到在一个已有的元素上直接copy覆盖更高效。keyi这样做:

std::copy(start, end, container.begin());

 在这里你在container的头部执行了copy-over(覆盖赋值)操作,但是,如果container的大小小于输入序列的长度N的话,这段代码会导致崩溃(crash)。

eg1:

   int a[3]={1,2,3};

   int b[3];
   std::copy(a,a+3,b);
   for(int j=0;j<3;j++)
     cout<< P>

eg2:

  vector temp(3);
 int a[3]={1,2,3};

 std::copy(a,a+3,&temp.front());
 cout<<<ENDL;
 for(int j=0;j
  cout<<<"  ?;
copy只负责复制,不负责申请空间,所以复制前必须有足够的空间

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++11 中,没有类似于 Rust 中 `std::option` 的标准库类型。但是 C++17 引入了 `std::optional` 类型,它的用法很类似于 Rust 中的 `std::option`。 `std::optional` 也表示一个值可能存在或者不存在的情况。它的定义如下: ```cpp template< class T > class optional { public: // 构造函数 constexpr optional() noexcept; constexpr optional( nullopt_t ) noexcept; constexpr optional( const optional& other ); constexpr optional( optional&& other ) noexcept( std::is_nothrow_move_constructible_v<T> ); template< class... Args > requires std::is_constructible_v<T, Args...> constexpr explicit optional( std::in_place_t, Args&&... args ); template< class U, class... Args > requires std::is_constructible_v<T, std::initializer_list<U>&, Args&&...> constexpr explicit optional( std::in_place_t, std::initializer_list<U> ilist, Args&&... args ); template< class U = T > requires std::is_copy_constructible_v<T> && std::is_convertible_v<const U&, T> constexpr optional( const U& value ); template< class U = T > requires std::is_move_constructible_v<T> && std::is_convertible_v<U&&, T> constexpr optional( U&& value ); template< class U > requires std::is_constructible_v<T, std::initializer_list<U>&> constexpr optional( std::initializer_list<U> ilist ); // 成员函数 constexpr T* operator->(); constexpr const T* operator->() const; constexpr T& operator*(); constexpr const T& operator*() const; constexpr explicit operator bool() const noexcept; constexpr T& value(); constexpr const T& value() const; template< class U > constexpr T value_or( U&& default_value ) const&; template< class U > constexpr T value_or( U&& default_value ) &&; void reset() noexcept; template< class... Args > requires std::is_constructible_v<T, Args&&...> T& emplace( Args&&... args ); template< class U, class... Args > requires std::is_constructible_v<T, std::initializer_list<U>&, Args&&...> T& emplace( std::initializer_list<U> ilist, Args&&... args ); void swap( optional& other ) noexcept( std::is_nothrow_move_constructible_v<T>&& noexcept(std::swap( std::declval<T&>(), std::declval<T&>() ) ) ); }; ``` 其中,`T` 是可能存在的值的类型。如果可能存在的值存在,那么 `optional` 对象就包含这个值;如果可能存在的值不存在,那么 `optional` 对象为空。 `std::optional` 提供了一些方法来处理可能存在的值。例如,可以使用 `value()` 方法获取可能存在的值,如果对象为空则会抛出 `std::bad_optional_access` 异常。也可以使用 `value_or()` 方法获取可能存在的值,如果对象为空则返回一个默认值。还可以使用 `has_value()` 方法来判断一个值是否存在。 例如,下面的代码演示了如何使用 `std::optional`: ```cpp #include <iostream> #include <optional> int main() { std::optional<int> x = 5; std::optional<int> y = std::nullopt; std::cout << "x is " << *x << std::endl; // 输出 x is 5 std::cout << "y is " << y.value_or(0) << std::endl; // 输出 y is 0 if (x.has_value()) { std::cout << "x has value" << std::endl; // 输出 x has value } if (!y.has_value()) { std::cout << "y does not have value" << std::endl; // 输出 y does not have value } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值