Aug 26, 2018
Learning C++ | Basics
C++ | Stack
A container adaptor keeps internally a container object as data, which is initialized by this constructor:
initialization constructor
Constructs a container adaptor whose internal container is initialized to a copy of ctnr.
move-initialization constructor
Constructs a container adaptor whose internal container acquires the value of ctnr by move-constructing it.
allocator constructor
Constructs a container adaptor whose internal container is constructed with alloc as argument.
initialization with allocator constructor
Constructs a container adaptor whose internal container is constructed with cntr and alloc as arguments.
move-initialization with allocator constructor
Constructs a container adaptor whose internal container is constructed with std::move(cntr) and alloc as arguments.
copy with allocator constructor
Constructs a container adaptor whose internal container is constructed with x's internal container as first argument and alloc as second.
move with allocator constructor
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()
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()
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()
Test whether container is empty Returns whether the stack is empty: i.e. whether its size is zero. This member function effectively calls member empty of the underlying container object.
// 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()
Returns a reference to the top element in the stack. 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 back of the underlying container object.
// 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()
Returns the length of the string, in terms of bytes. This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity. Note that string 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 string::size and string::length are synonyms and return the exact same value.
// 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;
}
Last updated