lintcode-solutions
  • LintCode 刷题笔记
  • 课程笔记
    • Backtracking
    • Binary Search
    • Divide & Conquer
    • Breadth First Search
    • Depth First Search
    • Linked List & Array
    • Two Pointers
    • Stack, Queue and Heap
    • Dynamic Programming
    • Two Pointers Advanced
    • Union Find and Trie
    • Dynamic Programming Advanced
  • 基础知识储备
    • Python
      • Stack and Queue
      • Namedtuple
      • Priority Queue
      • isinstance()
      • OrderedDict
      • set and frozen set
      • Counter
      • Heap
    • Bit Manipulation
    • Fenwick Tree or Binary Indexed Tree
    • Rabin-Karp Algorithm
    • Sort
      • Merge Sort
      • Quick Sort
      • Heap Sort
  • LintCode 练习题
    • Binary Search
  • OJ Review
    • Aug 7, 2018
    • Aug 8, 2018
    • Aug 9, 2018
    • Aug 13, 2018
    • Aug 17, 2018
    • Aug 19, 2018
    • Aug 24, 2018
    • Aug 26, 2018
    • Aug 27, 2018
    • Aug 29, 2018
    • Sep 1, 2018
    • Sep 2, 2018
    • Sep 3, 2018
    • Sep 4, 2018
    • Oct 28, 2018
    • Nov 13, 2018
Powered by GitBook
On this page
  • C++ | Stack
  • stack.size()
  • stack.push()
  • stack.empty()
  • stack.top()
  • C++ | string
  • string.length()
  1. OJ Review

Aug 26, 2018

Learning C++ | Basics

C++ | Stack

A container adaptor keeps internally a container object as data, which is initialized by this constructor:

  1. initialization constructor

    1. Constructs a container adaptor whose internal container is initialized to a copy of ctnr.

  2. move-initialization constructor

    1. Constructs a container adaptor whose internal container acquires the value of ctnr by move-constructing it.

  3. allocator constructor

    1. Constructs a container adaptor whose internal container is constructed with alloc as argument.

  4. initialization with allocator constructor

    1. Constructs a container adaptor whose internal container is constructed with cntr and alloc as arguments.

  5. move-initialization with allocator constructor

    1. Constructs a container adaptor whose internal container is constructed with std::move(cntr) and alloc as arguments.

  6. copy with allocator constructor

    1. Constructs a container adaptor whose internal container is constructed with x's internal container as first argument and alloc as second.

  7. move with allocator constructor

    1. Constructs a container adaptor whose internal container is constructed by moving x's internal container as first argument and passing alloc as second.

stack.size()

// constructing stacks
#include <iostream>       // std::cout
#include <stack>          // std::stack
#include <vector>         // std::vector
#include <deque>          // std::deque

int main ()
{
  std::deque<int> mydeque (3,100);          // deque with 3 elements
  std::vector<int> myvector (2,200);        // vector with 2 elements

  std::stack<int> first;                    // empty stack
  std::stack<int> second (mydeque);         // stack initialized to copy of deque

  std::stack<int,std::vector<int> > third;  // empty stack using vector
  std::stack<int,std::vector<int> > fourth (myvector);

  std::cout << "size of first: "  << first.size()  << '\n';
  std::cout << "size of second: " << second.size() << '\n';
  std::cout << "size of third: "  << third.size()  << '\n';
  std::cout << "size of fourth: " << fourth.size() << '\n';

  return 0;
}

stack.push()

Parameters

val: Value to which the inserted element is initialized. Member type value_type is the type of the elements in the container (defined as an alias of the first class template parameter, T).

// stack::push/pop
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;

  for (int i = 0; i < 5; ++i) mystack.push(i);

  std::cout << "Popping out elements...";
  while (!mystack.empty())
  {
     std::cout << ' ' << mystack.top();
     mystack.pop();
  }
  std::cout << '\n';

  return 0;
}

stack.empty()

// stack::empty
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;
  int sum (0);

  for (int i = 1;i <= 10;i++) mystack.push(i);

  while (!mystack.empty())
  {
     sum += mystack.top();
     mystack.pop();
  }

  std::cout << "total: " << sum << '\n';

  return 0;
}

stack.top()

// stack::top
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;

  mystack.push(10);
  mystack.push(20);

  mystack.top() -= 5;

  std::cout << "mystack.top() is now " << mystack.top() << '\n';

  return 0;
}

C++ | string

string.length()

// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}

PreviousAug 24, 2018NextAug 27, 2018

Last updated 6 years ago

Test whether container is empty Returns whether the is empty: i.e. whether its is zero. This member function effectively calls member of the underlying container object.

Returns a reference to the top element in the . Since stacks are last-in first-out containers, the top element is the last element inserted into the stack. This member function effectively calls member of the underlying container object.

Returns the length of the string, in terms of bytes. This is the number of actual bytes that conform the contents of the , which is not necessarily equal to its storage . Note that objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8). Both and string::length are synonyms and return the exact same value.

stack
size
empty
stack
back
string
capacity
string
string::size