Строки (C++): различия между версиями
Перейти к навигации
Перейти к поиску
(Новая страница: «== Строки == * [http://www.cplusplus.com/reference/string/string/ Строки (cplusplus)]») |
|||
Строка 1: | Строка 1: | ||
== Строки == | == Строки == | ||
* [http://www.cplusplus.com/reference/string/string/ Строки (cplusplus)] | * [http://www.cplusplus.com/reference/string/string/ Строки (cplusplus)] | ||
+ | |||
+ | <syntaxhighlight lang="c++" line> | ||
+ | #include <iostream> | ||
+ | |||
+ | int main() { | ||
+ | char ch = '0'; | ||
+ | std::cout << "Char " << ch << " has code " << (int)ch << std::endl; | ||
+ | |||
+ | int code = 55; | ||
+ | std::cout << "Char " << (char)code << " has code " << code << std::endl; | ||
+ | |||
+ | std::string str = "Hello"; | ||
+ | std::cout << "String " << str << " length = " << str.length() << std::endl; | ||
+ | |||
+ | std::size_t locl = str.find('l', 0); | ||
+ | if (locl != std::string::npos) { | ||
+ | std::cout << "String " << str << " has l character in position " << locl << std::endl; | ||
+ | } else { | ||
+ | std::cout << "String doesn't have l char.\n"; | ||
+ | } | ||
+ | |||
+ | std::size_t locz = str.find('z', 0); | ||
+ | if (locz != std::string::npos) { | ||
+ | std::cout << "String " << str << " has z character in position " << locz << std::endl; | ||
+ | } else { | ||
+ | std::cout << "String doesn't have z char.\n"; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
Версия 07:00, 26 июня 2021
Строки
1 #include <iostream>
2
3 int main() {
4 char ch = '0';
5 std::cout << "Char " << ch << " has code " << (int)ch << std::endl;
6
7 int code = 55;
8 std::cout << "Char " << (char)code << " has code " << code << std::endl;
9
10 std::string str = "Hello";
11 std::cout << "String " << str << " length = " << str.length() << std::endl;
12
13 std::size_t locl = str.find('l', 0);
14 if (locl != std::string::npos) {
15 std::cout << "String " << str << " has l character in position " << locl << std::endl;
16 } else {
17 std::cout << "String doesn't have l char.\n";
18 }
19
20 std::size_t locz = str.find('z', 0);
21 if (locz != std::string::npos) {
22 std::cout << "String " << str << " has z character in position " << locz << std::endl;
23 } else {
24 std::cout << "String doesn't have z char.\n";
25 }
26 }