Wednesday, December 27, 2017

C++11: Inline Function

A function declared constexpr is implicitly declared an inline function.



C++11: std::vector::shrink_to_fit()

C++11 added the vector function shrink_to_fit() to suggest to the system to reduce the capacity to size. Here is an example:

#include <iostream>
#include <vector>

int main()
{
  std::vector myVector;

  std::cout << myVector.capacity() << " "; // 15

  myVector.resize(1000);
  std::cout << myVector.capacity() << " "; // 1007

  myVector.clear();
  myVector.shrink_to_fit();
  std::cout << myVector.capacity() << " "; // 15

  std::cout << std::endl;
  return 0;
}
// Output: 0 1000 0
Reference: http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit

C++11: std::basic_string::shrink_to_fit()

C++11 added the string function shrink_to_fit() to suggest to the system to reduce the capacity to size. Here is an example:

#include <iostream>
#include <string>

int main()
{
  std::string myString;

  std::cout << myString.capacity() << " "; // 15

  myString.resize(1000);
  std::cout << myString.capacity() << " "; // 1007

  myString.clear();
  myString.shrink_to_fit();
  std::cout << myString.capacity() << " "; // 15

  std::cout << std::endl;
  return 0;
}
// Output: 15 1007 15
Reference: http://en.cppreference.com/w/cpp/string/basic_string/shrink_to_fit

C++98: std::istream_iterator

The istream_iterator template class wraps a stream and returns an input iterator. Here is an example:

#include <iostream>
#include <iterator>

int main()
{
  int myInt;
  std::istream_iterator<int> myIstreamIterator(std::cin);

  myInt = *myIstreamIterator;
  std::cout << myInt << " "; // 1

  myIstreamIterator++;
  myInt = *myIstreamIterator;
  std::cout << myInt << " "; // 2

  myIstreamIterator++;
  myInt = *myIstreamIterator;
  std::cout << myInt << " "; // 3

  std::cout << std::endl;  
  return 0;
}
// Input : 1 2 3
// Output: 1 2 3
Reference: http://www.cplusplus.com/reference/iterator/istream_iterator/

C++98: The boolalpha and nobool alpha Manipulators on cin

The boolalpha manipulator changes the way Boolean values are input from cin. Here is an example:

#include <iostream>

int main()
{
  bool dTrue   = false;
  bool dFalse  = false;
  bool szTrue  = false;
  bool szFalse = false;

  std::cin >> std::boolalpha;

  std::cin >> dTrue;
  std::cin >> dFalse;
  std::cin >> szTrue;
  std::cin >> szFalse;

  std::cout << dTrue   << " "; // 1
  std::cout << dFalse  << " "; // 0
  std::cout << szTrue  << " "; // 1
  std::cout << szFalse << " "; // 0

  std::cin >> std::noboolalpha;

  std::cin >> dTrue;
  std::cin >> dFalse;
  std::cin >> szTrue;
  std::cin >> szFalse;

  std::cout << dTrue   << " "; // 1
  std::cout << dFalse  << " "; // 0
  std::cout << szTrue  << " "; // 1
  std::cout << szFalse << " "; // 0

  std::cout << std::endl;
  return 0;
}
// Input : true false true false 1 0 1 0
// Output: 1 0 1 0 1 0 1 0
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 617.

C++98: The boolalpha and noboolalpha Manipulators on cout

The boolalpha manipulator changes the way Boolean values are output to cout. Here is an example:

#include <iostream>

int main()
{
  std::cout << true  << " "; // 1
  std::cout << false << " "; // 0

  std::cout << std::boolalpha;

  std::cout << true  << " "; // true
  std::cout << false << " "; // false

  std::cout << std::noboolalpha;

  std::cout << true  << " "; // 1
  std::cout << false << " "; // 0

  std::cout << std::endl;
  return 0;
}
// Output: 1 0 true false 1 0
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 617.

C++98: Standard Streams

There are eight C++ standard streams. The four narrow character streams are: cin, cout, cerr, and clog. The four wide-character streams are wcin, wcout, wcerr, and wclog.

Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 682.