• <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • C++11 <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • C++11 <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)
  • <cmath> (math.h)
  • <csetjmp> (setjmp.h)
  • <csignal> (signal.h)
  • <cstdarg> (stdarg.h)
  • C++11 <cstdbool> (stdbool.h)
  • <cstddef> (stddef.h)
  • C++11 <cstdint> (stdint.h)
  • <cstdio> (stdio.h)
  • <cstdlib> (stdlib.h)
  • <cstring> (string.h)
  • C++11 <ctgmath> (tgmath.h)
  • <ctime> (time.h)
  • C++11 <cuchar> (uchar.h)
  • <cwchar> (wchar.h)
  • <cwctype> (wctype.h)

Containers:

  • C++11 <array>
  • <deque>
  • C++11 <forward_list>
  • <list>
  • <map>
  • <queue>
  • <set>
  • <stack>
  • C++11 <unordered_map>
  • C++11 <unordered_set>
  • <vector>

Input/Output:

  • <fstream>
  • <iomanip>
  • <ios>
  • <iosfwd>
  • <iostream>
  • <istream>
  • <ostream>
  • <sstream>
  • <streambuf>

Multi-threading:

  • C++11 <atomic>
  • C++11 <condition_variable>
  • C++11 <future>
  • C++11 <mutex>
  • C++11 <thread>
  • <algorithm>
  • <bitset>
  • C++11 <chrono>
  • C++11 <codecvt>
  • <complex>
  • <exception>
  • <functional>
  • C++11 <initializer_list>
  • <iterator>
  • <limits>
  • <locale>
  • <memory>
  • <new>
  • <numeric>
  • C++11 <random>
  • C++11 <ratio>
  • C++11 <regex>
  • <stdexcept>
  • <string>
  • C++11 <system_error>
  • C++11 <tuple>
  • C++11 <type_traits>
  • C++11 <typeindex>
  • <typeinfo>
  • <utility>
  • <valarray>
  • vector<bool>
  • vector::~vector
  • vector::vector

member functions

  • vector::assign
  • vector::back
  • vector::begin
  • vector::capacity
  • C++11 vector::cbegin
  • C++11 vector::cend
  • vector::clear
  • C++11 vector::crbegin
  • C++11 vector::crend
  • C++11 vector::data
  • C++11 vector::emplace
  • C++11 vector::emplace_back
  • vector::empty
  • vector::end
  • vector::erase
  • vector::front
  • vector::get_allocator
  • vector::insert
  • vector::max_size
  • vector::operator[]
  • vector::operator=
  • vector::pop_back
  • vector::push_back
  • vector::rbegin
  • vector::rend
  • vector::reserve
  • vector::resize
  • C++11 vector::shrink_to_fit
  • vector::size
  • vector::swap

non-member overloads

  • relational operators (vector)
  • swap (vector)

std:: vector ::operator=

Return value, iterator validity, exception safety.

  • Standard Template Library
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators

vector::operator= and vector::operator[ ] in C++ STL

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.  

This operator is used to assign new contents to the container by replacing the existing contents.  It also modifies the size according to the new contents.

Syntax :  

Examples:  

Errors and Exceptions 1. If the containers are of different types, an error is thrown.  2. It has a basic no exception throw guarantee otherwise.

Time Complexity – Linear O(N)

Output: 

This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of vector, while this operator causes undefined behavior .

Syntax :   

Examples: 

Errors and Exceptions 1. If the position is not present in the vector, it shows undefined behavior.  2. It has a no exception throw guarantee otherwise. 

Time Complexity – Constant O(1)

Application   Given a vector of integers, print all the integers present at odd positions. 

Algorithm   1. Run a loop till the size of the vector.  2. Check if the position is not divisible by 2, then print the element at that position.

  Let us see the differences in a tabular form -:

Similar Reads

Improve your coding skills with practice.

 alt=

What kind of Experience do you want to share?

cppreference.com

Std:: vector.

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.

The storage of the vector is handled automatically, being expanded as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. The total amount of allocated memory can be queried using capacity() function. Extra memory can be returned to the system via a call to shrink_to_fit() [1] .

Reallocations are usually costly operations in terms of performance. The reserve() function can be used to eliminate reallocations if the number of elements is known beforehand.

The complexity (efficiency) of common operations on vectors is as follows:

  • Random access - constant 𝓞(1) .
  • Insertion or removal of elements at the end - amortized constant 𝓞(1) .
  • Insertion or removal of elements - linear in the distance to the end of the vector 𝓞(n) .

std::vector (for T other than bool ) meets the requirements of Container , AllocatorAwareContainer (since C++11) , SequenceContainer , ContiguousContainer (since C++17) and ReversibleContainer .

  • ↑ In libstdc++, shrink_to_fit() is not available in C++98 mode.

[ edit ] Template parameters

[edit]

[ edit ] Specializations

The standard library provides a specialization of std::vector for the type bool , which may be optimized for space efficiency.

[ edit ] Iterator invalidation

[ edit ] member types, [ edit ] member functions, [ edit ] non-member functions, [ edit ] notes, [ edit ] example, [ edit ] defect reports.

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

[ edit ] See also

  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 2 August 2024, at 21:20.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

IMAGES

  1. C++ : Does std::vector::assign/std::vector::operator=(const&) guarantee

    c std vector assignment operator

  2. Assignment Operators in C Example

    c std vector assignment operator

  3. Assignment Operators in C

    c std vector assignment operator

  4. Assignment operators in C++ programming

    c std vector assignment operator

  5. What is assignment operator in C with example?

    c std vector assignment operator

  6. Discovering C Operators: An Overview with Types and Examples!

    c std vector assignment operator

VIDEO

  1. 2016 ATLAS COPCO CA2500D For Sale

  2. C++

  3. Assignment Operator in C Programming

  4. How Vectors Allocate Memory in C++

  5. Augmented assignment operators in C

  6. L-8 vector assignment