PrevUpHomeNext

day-22: c++ STL: std::string


STL is the c++ Standard Template Library. Most c++ compilers have implemented its own STL. So if you have installed a mainstream c++ compiler, you can use STL directly without additional setup. Microsoft visual c++ (visualcpp), GNU c++ compiler (gcc), clang c++ compiler are all such compilers that have implemented STL.

std::string

std::string is the c++ string from STL. You have learned it at day-03

Include:

#include <string>

Because std::string is widely used almost anywhere by c++ libraries, commonly you don't have to include <string>, other headers might have already included it.

Create string

#include <iostream>

int main()
{
	std::string str1;	// Create an empty string
	std::string str2 = "Hello";	// Create string "Hello"
	std::string str3{"c++"};	// Create string "c++"
	std::cout << str1 << '\n' << str2 << '\n' << str3 << '\n';
}

Method: .data()

The method .data() of std::string returns const char * style string.

const char * style string is the c++ raw string.

std::string str = "hello c++";
const char * str_data = str.data();	// Method .data()
std::cout << str << '\n' << str_data << std::endl;

Method: .size()

The method .size() of std::string returns how many characters that the string has.

std::string str = "Hello c++!!";
auto size = str.size();	// Method .size()		Returns 11

Method: .operator+, .operator+=

The method .operator+, .operator+= of sd::string can join two c++ std::string together as one string.

std::string str = "Hello";
str = str + " world";	// .operator+
str += "!!!";	// .operator+=
std::cout << str << std::endl;	// Hello world!!!

Method .push_back

The method .push_back of std::string appends a single character to the end of the string.

std::string str = "c++ boo";
str.push_back('s');	// appends a single character 's'
str.push_back('t');	// appends a single character 't'
std::cout << str << std::endl;	// c++ boost

Method .operator[]

The method .operator[] of std::string access an element (it is a character) at specific location.

std::string str = "Hello c++";
std::cout << str[1] << std::endl;	// e

Method .contains

The method .contains of std::string checks if the string contains a specific sub-string.

std::string str = "Hello c++";
bool r = str.contains("ll");	// true
bool r2 = str.contains("le");	// false

Method .starts_with

The method .starts_with of std::string checks if the string starts with a specific string.

std::string str = "c++ Hello...";
bool r1 = str.starts_with("c++");	// true
bool r2 = str.starts_with("++");	// false

Method .ends_with

The method .ends_with of std::string checks if the string ends with a specific string.

std::string str = "c++ Hello...";
bool r1 = str.ends_with("...");	// true
bool r2 = str.ends_with("....");	// false

Method .substr

The method .substr of std::string returns a sub-string of the string.

#include <iostream>

int main()
{
	std::string str = "Hello woRld";

	// Get a sub-string from position 2, with 5 characters.
	std::string s1 = str.substr(2, 5);

	// Get a sub-string from position 2, to the end.
	std::string s2 = str.substr(2);

	std::cout << s1 << std::endl;	// llo w
	std::cout << s2 << std::endl;	// llo woRld
}

c++ iterator - std::string

Yeah, you read that right. std::string has related iterator, just like the std::vector:
std::vector iterator

std::string is also a vector, just its value data type is character type, and it is desgiend for string specially.

c++ example:

#include <iostream>

// This include can be omitted, as std::string is widely used and included by many headers.
// #include <string>

int main()
{
	const std::string str = "Hello, welcome to c++ programming!";

	const auto itr1 = str.begin();
	const auto itr2 = str.end();

	auto itr3 = str.begin();
	itr3++;
	itr3 += 3;

	char x = *itr3;	// Result is 'o'

	auto distance = itr2 - itr3;	// Get the distance between itr2 and itr3

	std::cout << x << '\n' << distance << std::endl;
}







Written on Dec 07, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz


PrevUpHomeNext