Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::forward_list<T,Allocator>::forward_list

      From cppreference.com
      <cpp‎ |container‎ |forward list
       
       
       
      std::forward_list
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      forward_list(): forward_list(Allocator()){}
      (1)(constexpr since C++26)
      explicit forward_list(const Allocator& alloc);
      (2)(constexpr since C++26)
      explicit forward_list( size_type count,
                             const Allocator& alloc= Allocator());
      (3)(constexpr since C++26)
      forward_list( size_type count,const T& value,
             const Allocator& alloc= Allocator());
      (4)(constexpr since C++26)
      template<class InputIt>

      forward_list( InputIt first, InputIt last,

                   const Allocator& alloc= Allocator());
      (5)(constexpr since C++26)
      template<container-compatible-range<T> R>

      forward_list(std::from_range_t, R&& rg,

                   const Allocator& alloc= Allocator());
      (6)(since C++23)
      (constexpr since C++26)
      forward_list(const forward_list& other);
      (7)(constexpr since C++26)
      forward_list( forward_list&& other);
      (8)(constexpr since C++26)
      (9)
      forward_list(const forward_list& other,const Allocator& alloc);
      (until C++23)
      forward_list(const forward_list& other,
                   conststd::type_identity_t<Allocator>& alloc);
      (since C++23)
      (constexpr since C++26)
      (10)
      forward_list( forward_list&& other,const Allocator& alloc);
      (until C++23)
      forward_list( forward_list&& other,
                   conststd::type_identity_t<Allocator>& alloc);
      (since C++23)
      (constexpr since C++26)
      forward_list(std::initializer_list<T> init,
                   const Allocator& alloc= Allocator());
      (11)(constexpr since C++26)

      Constructs a newforward_list from a variety of data sources, optionally using a user supplied allocatoralloc.

      1) The default constructor. Constructs an emptyforward_list with a default-constructed allocator.
      IfAllocator is notDefaultConstructible, the behavior is undefined.
      2) Constructs an emptyforward_list with the given allocatoralloc.
      3) Constructs aforward_list withcount default-inserted objects ofT. No copies are made.
      IfT is notDefaultInsertable intoforward_list, the behavior is undefined.
      4) Constructs aforward_list withcount copies of elements with valuevalue.
      IfT is notCopyInsertable intoforward_list, the behavior is undefined.
      5) Constructs aforward_list with the contents of the range[firstlast). Each iterator in[firstlast) is dereferenced exactly once.
      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.
      IfT is notEmplaceConstructible intoforward_list from*first, the behavior is undefined.
      6) Constructs aforward_list with the contents of the rangerg. Each iterator inrg is dereferenced exactly once.
      IfT is notEmplaceConstructible intoforward_list from*ranges::begin(rg), the behavior is undefined.
      7) The copy constructor. Constructs aforward_list with the contents ofother. The allocator is obtained as if by callingstd::allocator_traits<Allocator>::select_on_container_copy_construction
             (other.get_allocator())
      .
      8) The move constructor. Constructs aforward_list with the contents ofother. The allocator is obtained by move construction fromother.get_allocator().
      9) Same as the copy constructor, except thatalloc is used as the allocator.
      IfT is notCopyInsertable intoforward_list, the behavior is undefined.
      10) Same as the move constructor, except thatalloc is used as the allocator.
      IfT is notMoveInsertable intoforward_list, the behavior is undefined.
      11) Equivalent toforward_list(il.begin(), il.end(), alloc).

      Contents

      [edit]Parameters

      alloc - allocator to use for all memory allocations of this container
      count - the size of the container
      value - the value to initialize elements of the container with
      first, last - the pair of iterators defining the sourcerange of elements to copy
      other - another container to be used as source to initialize the elements of the container with
      init - initializer list to initialize the elements of the container with
      rg - a container compatible range

      [edit]Complexity

      1,2) Constant.
      3,4) Linear incount.
      5) Linear instd::distance(first, last).
      6) Linear inranges::distance(rg).
      7) Linear inother.size().
      8) Constant.
      9) Linear inother.size().
      10) Linear inother.size() ifalloc!= other.get_allocator(), otherwise constant.
      11) Linear ininit.size().

      [edit]Exceptions

      Calls toAllocator::allocate may throw.

      [edit]Notes

      After container move construction (overload(8)), references, pointers, and iterators (other than the end iterator) toother remain valid, but refer to elements that are now in*this. The current standard makes this guarantee via the blanket statement in[container.reqmts]/67, and a more direct guarantee is under consideration viaLWG issue 2321.

      Feature-test macroValueStdFeature
      __cpp_lib_containers_ranges202202L(C++23)Ranges-aware construction and insertion; overload(6)

      [edit]Example

      Run this code
      #include <forward_list>#include <iostream>#include <string> template<typename T>std::ostream& operator<<(std::ostream& s,conststd::forward_list<T>& v){    s.put('{');for(char comma[]{'\0',' ','\0'};constauto& e: v)        s<< comma<< e, comma[0]=',';return s<<"}\n";} int main(){// C++11 initializer list syntax:std::forward_list<std::string> words1{"the","frogurt","is","also","cursed"};std::cout<<"1: "<< words1; // words2 == words1std::forward_list<std::string> words2(words1.begin(), words1.end());std::cout<<"2: "<< words2; // words3 == words1std::forward_list<std::string> words3(words1);std::cout<<"3: "<< words3; // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}std::forward_list<std::string> words4(5,"Mo");std::cout<<"4: "<< words4; constauto rg={"cat","cow","crow"};#ifdef __cpp_lib_containers_rangesstd::forward_list<std::string> words5(std::from_range, rg);// overload (6)#elsestd::forward_list<std::string> words5(rg.begin(), rg.end());// overload (5)#endifstd::cout<<"5: "<< words5;}

      Output:

      1: {the, frogurt, is, also, cursed}2: {the, frogurt, is, also, cursed}3: {the, frogurt, is, also, cursed}4: {Mo, Mo, Mo, Mo, Mo}5: {cat, cow, crow}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2193C++11the default constructor was explicitmade non-explicit
      LWG 2210C++11overload(3) did not have an allocator parameteradded the parameter
      N3346C++11for overload(3), the elements in
      the container were value-initialized
      they are default-inserted

      [edit]See also

      assigns values to the container
      (public member function)[edit]
      assigns values to the container
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/forward_list/forward_list&oldid=183078"

      [8]ページ先頭

      ©2009-2025 Movatter.jp