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

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

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Welcome to Beginning C++17. This is a revised and updated version of Ivor Horton’s original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise. We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, you’ll be learning a language that is already used by millions and that provides the capability for application development in just about any context. C++ is very powerful. Arguably, it’s more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then you’ll never have to look for another pocketknife again. C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. In essence, we have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade in particular has seen the ris
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值