Monday, October 23, 2017

C++17: std::string_view

C++17 added the class template string_view, which is a kind of lightweight constant string. Here is an example:

#include <iostream>
#include <string>
#include <string_view>

int main()
{
  std::string_view myStringView("A rat in the house.");
  std::string myString;

  myString = myStringView;
  
  std::cout << myString     << " ";
  std::cout << myStringView << " ";

  std::cout << std::endl;
  return 0;
}
// Output: A rat in the house. A rat in the house.
References:
https://en.wikipedia.org/wiki/C%2B%2B17
http://en.cppreference.com/w/cpp/string/basic_string_view

No comments:

Post a Comment