String Length

beginner c++14 strings

The std::string class from the Standard Library has both the size() and length() methods which return the length of the string in bytes. The two methods are identical except for their naming of course.

#include <iostream>
#include <string>

int main() {
  std::string s{"Hello cppbyexample.com"};

  std::cout << s.size() << "\n";
  std::cout << s.length() << "\n";
}
22
22

Why Both size() and length()?

All of the containers in C++’s Standard Library provide a size() method which returns the number of elements in the container. In C++ if you want to ask a std::vector, std::map, std::stack, std::string, or any other container how many elements they contain you only need to remember size. This can be convenient when writing generic algorithms that work on containers but don’t really care what specific type of container.

#include <iostream>
#include <string>
#include <vector>

// Middle element uses C++14 features (auto return type)
template <typename T>
auto middle_element(const T& t) {
  auto midpoint = t.begin() + t.size() / 2;
  return *midpoint;
}

int main() {
  std::string s{"Hello cppbyexample.com"};
  std::cout << middle_element(s) << "\n";

  std::vector<int> v{1, 2, 3, 4, 5, 6, 7};
  std::cout << middle_element(v) << "\n";
}
e
4

ASCII, UTF-8 and std::string

In C++ strings are simply arrays of bytes (char) with no specific encoding. This means size() works well for English text and the ASCII character set but also that C++ std::string has no understanding of the number of codepoints (visual symbols) in a UTF-8 string. So, while std::string can store UTF-8 text (as an array of bytes [char]) it can’t tell you how many codepoints that text contains.

#include <iostream>
#include <string>

int main() {
  // u8 means UTF-8 literal string
  std::string s{u8"สวัสดีจ้า cppbyexample.com"};

  std::cout << s << "\n";
  std::cout << s.size() << "\n";
  std::cout << s.length() << "\n";
}
สวัสดีจ้า cppbyexample.com
44
44


For more C++ By Example, click here.