《Beginning C++17》-学习笔记-Chapter 07-Working with Strings

本文是《 Beginning C++17》一书中Chapter 07的学习笔记,主要探讨了在C++17中如何有效地使用和操作字符串,包括字符串的创建、基本操作、字符串流以及C++17新特性在字符串处理中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#include <iostream>
#include <string>//This header of the C++ Standard Library defines the std::string type
/*Type string is a compound type, which is a type that’s a
composite of several data items that are ultimately defined in terms of fundamental types of data. Next to the
characters that make up the string it represents, a string object contains other data as well, such as number
of characters in the string. */
int main()
{
	std::string empty; // An empty string that has zero length.
	std::string proverb{ "Many a mickle makes a muckle." }; // a string object defined and initialized with a string literal.
	/*The character array encapsulated by a string object is always terminated by a null character to assure compatibility
	with the numerous existing functions that expect C-style strings.*/
	std::cout << proverb.length() <<std::endl; // Outputs 29; This length never includes the string termination character

	std::string part_literal{ "Least said soonest mended.", 5 }; // "Least"
	std::cout << part_literal << std::endl;

	std::string sleeping(6, 'z');//do not use curly braces
	std::cout << sleeping << std::endl;//Output zzzzzz

	std::string sentence{ proverb };
	std::cout << sentence << std::endl;

	std::string phrase{ proverb, 0, 13 }; // Initialize with 13 characters starting at index 0
	std::cout << phrase << std::endl;

	std::string name;
	std::cout << "enter your name: ";
	std::cin >> name; // Pressing Enter ends input
	std::cout << name << std::endl;
	/*This reads characters up to the first whitespace character, which ends the input process.*/

	std::string adjective{ "adj" }; // Defines adjective
	std::string word{ "move" }; // Defines word
	word = adjective; // Modifies word
	adjective = "beautiful"; // Modifies adjective
	std::cout << word << std::endl;
	std::cout << adjective << std::endl;

	std::string description{ name + " is " + adjective + "." };//Concatenating Strings
	/* concatenate string literals with string objects using the + operator. */
	/*you can’t concatenate two string literals using the + operator. One of the two operands of the +
operator must always be an object of type string. */

	std::cout << description << std::endl;

	std::string a_sentence{ "This" " " "is" " " "a" " " "sentence."}; 
	// The compiler will concatenate two or more string literals in sequence into a single literal
	std::cout << a_sentence << std::endl;

	using namespace std::string_literals;
	std::string another_sentence{ "This" + " is a "s + "sentence." };
	// appending the letter s to a string literal turns it into a std::string object
	std::cout << another_sentence << std::endl;

}
// Concatenating strings with append function
#include <iostream>
#include <string>

int main()
{
	std::string sentence{ "The first letter of the English Alphabet is: " };   // Create basic sentence

	sentence.append("a");

	std::string punctuations("~?!&()");
	sentence.append(punctuations, 2, 1); // Appends "!"; index starts from 0
	sentence.append(3, '!'); // Appends 3 other "!"
	std::cout << sentence << std::endl;
}
#include <iostream>
#include <string>

int main()
{
	std::string sentence{ "This sentence is appended with letter:" };   // Create basic sentence
	sentence += ',' + ' ';

	std::cout << sentence << std::endl;             // Output the sentence
	/* The ASCII code for ',' is 44, and that of the ' ' character is 32. Their sum, 32 + 44,
therefore equals 76, which happens to be the ASCII code for the capital letter 'L'.*/
}
#include <iostream>
#include <string>

int main()
{
	std::string sentence{ "ASCII code 69 is for letter:" }; // E
	sentence += 69;
	std::cout << sentence << std::endl; 
}
#include <iostream>
#include <string>

int main()
{
	const std::string result_string{ "Pi is: " };
	double result = 3.1415;
	std::cout << (result_string + std::to_string(result)) << std::endl;
}
#include <iostream>
#include <string>
#include <cctype>		// for std::isalpha() and tolower()

int main()
{
	std::string text;                              // Stores the input
	std::cout << "Enter a line of text:\n";
	std::getline(std::cin, text);                  // Read a line including spaces

	std::cout << "The line of text you entered is as follows:" << std::endl;
	std::cout << text << std::endl;
/*The getline() is declared in the string header. It reads characters from
the stream specified by the first argument, cin in this case, until a newline character is read, and the result is stored in the string object specified by the second argument, which is text in this case.*/
}
#include <iostream>
#include <string>
#include <cctype>

int main()
{
	std::string text;                              // Stores the input
	std::cout << "Enter a line of text:\n";

	std::getline(std::cin, tex
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值