string at c++ code example
Example 1: string in cpp
#include <string>
string greeting = "Hello";
Example 2: c++ string
#include <string>
std::string myString = "Hello, World!";
Example 3: c++ string
#include <string>
#include <iostream>
#include <type_traits>
#include <cstring>
int main() {
std::string str = "Hello, there";
std::cout << std::boolalpha
<< str.capacity() << ", " << str.size() << ", " << std::strlen(str.data())
<< '\n' << std::is_same_v<std::string, std::basic_string<char>>
<< '\n' << str.front() + str.substr(1, 10) + str.back()
<< '\n' << str[0]
<< '\n';
str += "!";
std::cout << str << '\n';
str.erase(4, 4);
str.pop_back();
str.insert(4, " ");
std::cout << str << '\n';
}
Example 4: string in cpp
#include <string>
#include <iostream>
int main()
{
string helloWorld = "Hello World!";
std::cout << helloWorld;
string namePerson{};
getline(cin, namePerson);
std::cout << namePerson;
}
Example 5: how to access the element of string in c++
string myString = "Hello";
cout << myString[0];