c++11 container liber: std::queue std::map std::vector std::unique_ptr

来源:互联网 发布:微商管理系统源码 编辑:程序博客网 时间:2024/06/07 11:11




typedef std::map<std::unique_ptr<TUnixSocket>,


std::unique_ptr<TPacketPayloads>> TSocketPacketMap;


typedef std::vector<std::pair<std::uint64_t, std::uint64_t>> TBufferRange;


std::unique_ptr<TSocketPacketMap> SocketPacketMap;
std::unique_ptr<TBufferRange> BufferRanges;


std::find_if



std::shared_ptr

std::unique_ptr

std::make_shared







  class TMediaSourceBuffer {
  public:
    std::string Id;
    std::string Type;
    double EndTimeRange;
    std::string Codecs;
    TUnixSocket ListenSocket;
    std::string ListenSocketPath;


    typedef std::deque<std::unique_ptr<TPacketPayload>> TPacketPayloads;
    typedef std::vector<std::pair<double, double>> TBufferRange;
    std::unique_ptr<TUnixSocket> StreamSocket;
    TPacketPayloads PacketPayloads;
    TBufferRange BufferRanges;


    TMediaSourceBuffer(std::string id,
                       std::string type = "",
                       double endTimeRange = 0.0,
                       std::string codecs = "");
    TMediaSourceBuffer() = delete;


  };

  std::vector<std::unique_ptr<TMediaSourceBuffer>> MediaSourceBuffers;




audo and for:

  for (auto &msb : MediaSourceBuffers) {
    if (msb->StreamSocket && msb->StreamSocket->IsOpen()) {
      KTRACE1("Send data to %d, PacketPayloads.size(): %d",
              msb->StreamSocket->GetFD(),
              msb->PacketPayloads.size());


      while (!msb->PacketPayloads.empty()) {
        if ((*msb->PacketPayloads.begin())->Send(*msb->StreamSocket)) {
          msb->PacketPayloads.pop_front();
        }
        else {
          KTRACE3(">> socket buffer is full, Type: %s ", msb->Type.c_str());
          break;
        }
      }



auto , vector, push_back, move

  auto msb = std::unique_ptr<TMediaSourceBuffer>(
    new TMediaSourceBuffer(Utils::GetStringFromEKString(id),
                           Utils::GetStringFromEKString(type),
                           0.0));

  MediaSourceBuffers.push_back(std::move(msb));



find_if,  Lambda Functions and Algorithms

  auto msb = std::find_if(
    MediaSourceBuffers.begin(),
    MediaSourceBuffers.end(),
    [&sourceId] (std::unique_ptr<TMediaSourceBuffer> &each)
      { return each->Id == sourceId;});

  if (msb == MediaSourceBuffers.end()) {
    KTRACE1("Invalid media source id");
    return false;
  }

  numRanges = (*msb)->BufferRanges.size();
  KTRACE1("<< sourceId: %s, type: %s, numRanges: %u, ",
          sourceId.c_str(), (*msb)->Type.c_str(), numRanges);
  return true;




unique_ptr  push_back

  (*msb)->PacketPayloads.push_back(
    std::unique_ptr<TPacketPayload>(new TPacketPayload(
                                      (uint8_t*)data,
                                      length,
                                      nullptr)));









Containers library


http://en.cppreference.com/w/cpp/container


Containers library

 
C++
 
Containers library
 

The Containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are three classes of containers -- sequence containers, associative containers, and unordered associative containers -- each of which is designed to support a different set of operations.

The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with similar properties to pointers).

Most containers have at least several member functions in common, and share functionalities. Which container is the best for the particular application depends not only on the offered functionality, but also on its efficiency for different workloads.

Sequence containers

Sequence containers implement data structures which can be accessed sequentially.

array
(since C++11)
static contiguous array 
(class template)
vector
dynamic contiguous array 
(class template)
deque
double-ended queue 
(class template)
forward_list
(since C++11)
singly-linked list 
(class template)
list
doubly-linked list 
(class template)

Associative containers

Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).

set
collection of unique keys, sorted by keys 
(class template)
map
collection of key-value pairs, sorted by keys, keys are unique 
(class template)
multiset
collection of keys, sorted by keys 
(class template)
multimap
collection of key-value pairs, sorted by keys 
(class template)

Unordered associative containers

Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1)amortized, O(n) worst-case complexity).

unordered_set
(since C++11)
collection of unique keys, hashed by keys 
(class template)
unordered_map
(since C++11)
collection of key-value pairs, hashed by keys, keys are unique 
(class template)
unordered_multiset
(since C++11)
collection of keys, hashed by keys 
(class template)
unordered_multimap
(since C++11)
collection of key-value pairs, hashed by keys 
(class template)

Container adaptors

Container adaptors provide a different interface for sequential containers.

stack
adapts a container to provide stack (LIFO data structure) 
(class template)
queue
adapts a container to provide queue (FIFO data structure) 
(class template)
priority_queue
adapts a container to provide priority queue 
(class template)

Iterator invalidation

 This section is incomplete

Thread safety

  1. All container functions can be called concurrently by different threads on different containers. More generally, the C++ standard library functions do not read objects accessible by other threads unless those objects are directly or indirectly accessible via the function arguments, including the this pointer.
  2. All const member functions can be called concurrently by different threads on the same container. In addition, the member functions begin()end()rbegin()rend()front()back()data(),find()lower_bound()upper_bound()equal_range()at(), and, except in associative containers, operator[], behave as const for the purposes of thread safety (that is, they can also be called concurrently by different threads on the same container). More generally, the C++ standard library functions do not modify objects unless those objects are accessible, directly or indirectly, via the function's non-const arguments, including the this pointer.
  3. Different elements in the same container can be modified concurrently by different threads, except for the elements of std::vector<bool> (for example, a vector of std::future objects can be receiving values from multiple threads).
  4. Iterator operations (e.g. incrementing an iterator) read, but do not modify the underlying container, and may be executed concurrently with operations on other iterators on the same container, with the const member functions, or reads from the elements. Container operations that invalidate any iterators modify the container and cannot be executed concurrently with any operations on existing iterators even if those iterators are not invalidated.
  5. Elements of the same container can be modified concurrently with those member functions that are not specified to access these elements. More generally, the C++ standard library functions do not read objects indirectly accessible through their arguments (including other elements of a container) except when required by its specification.
  6. In any case, container operations (as well as algorithms, or any other C++ standard library functions) may be parallelized internally as long as this does not change the user-visible results (e.g.std::transform may be parallelized, but not std::for_each which is specified to visit each element of a sequence in order)
(since C++11)

Member function table

 - functions present in C++03 - functions present since C++11
 Sequence containersAssociative containersUnordered associative containersContainer adaptorsHeader<array><vector><deque><forward_list><list><set><map><unordered_set><unordered_map><stack><queue>Container
array
vector
deque
forward_list
list
set
multiset
map
multimap
unordered_set
unordered_multiset
unordered_map
unordered_multimap
stack
queue
priority_queue
 
(constructor)
(implicit)
vector
deque
forward_list
list
set
multiset
map
multimap
unordered_set
unordered_multiset
unordered_map
unordered_multimap
stack
queue
priority_queue
(destructor)
(implicit)
~vector
~deque
~forward_list
~list
~set
~multiset
~map
~multimap
~unordered_set
~unordered_multiset
~unordered_map
~unordered_multimap
~stack
~queue
~priority_queue
operator=
(implicit)
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
assign
assign
assign
assign
assign
Iteratorsbegincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
begincbegin
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
endcend
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rbegincrbegin
rendcrend
rendcrend
rendcrend
rendcrend
rendcrend
rendcrend
rendcrend
rendcrend
rendcrend
Element 
access
at
at
at
at
at
at
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
front
front
front
front
front
front
front
top
back
back
back
back
back
top
back
Capacity
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
size
size
size
size
size
size
size
size
size
size
size
size
size
size
size
size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
resize
resize
resize
resize
resize
capacity
capacity
reserve
reserve
reserve
reserve
reserve
reserve
shrink_to_fit
shrink_to_fit
shrink_to_fit
Modifiers
clear
clear
clear
clear
clear
clear
clear
clear
clear
clear
clear
clear
clear
insert
insert
insert
insert_after
insert
insert
insert
insert
insert
insert
insert
insert
insert
emplace
emplace
emplace
emplace_after
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
erase
erase
erase
erase_after
erase
erase
erase
erase
erase
erase
erase
erase
erase
push_front
push_front
push_front
push_front
emplace_front
emplace_front
emplace_front
emplace_front
pop_front
pop_front
pop_front
pop_front
pop
push_back
push_back
push_back
push_back
push
push
push
emplace_back
emplace_back
emplace_back
emplace_back
emplace
emplace
emplace
pop_back
pop_back
pop_back
pop_back
pop
pop
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
swap
List operations
merge
merge
merge
splice
splice_after
splice
remove
remove
remove
remove_if
remove_if
remove_if
reverse
reverse
reverse
unique
unique
unique
sort
sort
sort
Lookup
count
count
count
count
count
count
count
count
count
find
find
find
find
find
find
find
find
find
lower_bound
lower_bound
lower_bound
lower_bound
lower_bound
upper_bound
upper_bound
upper_bound
upper_bound
upper_bound
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
Observers
key_comp
key_comp
key_comp
key_comp
key_comp
value_comp
value_comp
value_comp
value_comp
value_comp
hash_function
hash_function
hash_function
hash_function
hash_function
key_eq
key_eq
key_eq
key_eq
key_eq
Allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
Container
array
vector
deque
forward_list
list
set
multiset
map
multimap
unordered_set
unordered_multiset
unordered_map
unordered_multimap
stack
queue
priority_queue
 Sequence containersAssociative containersUnordered associative containersContainer adaptors

(A PDF version of this table is available at File:container-library-overview-2012-12-27.pdf.)





0 0
原创粉丝点击