⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个关于标准string类的C++程序
💻 CPP
字号:
#include <iostream>

using namespace std;

#include "head.h"

int main()
{
	String s1( "happy" );
	String s2( " birthday" );
	String s3;

	//test overloaded equality and relational operators
	cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2
		 << "\"; s3 is \"" << s3 << '\"'
		 <<"\n\nThe results of comparing s2 and s1:"
		 << "\ns2 == s1 yields "
		 << ( s2 == s1 ? "true" : "false")
		 << "\ns2 !=s1 yields "
		 << ( s2 != s1 ? "true" : "false")
		 << "\ns2 > s1 yields "
		 << ( s2 > s1 ? "true" : "false")
		 << "\ns2 < s1 yields "
		 << ( s2 <s1 ? "true" : "false")
		 << "\ns2 >= s1 yields "
		 << ( s2 >= s1 ? "true" : "false")
		 << "\ns2 <= s1 yields "
		 << ( s2 <= s1 ? "true" : "false");

	cout << "\n\nTesting !s3:\n";

	if ( !s3 )
	{
		cout << "s3 is empty; assigning s1 to s3;\n";
		s3 = s1;
		cout << "s3 is \"" << s3 << "\"";
	}


	cout << "\n\ns1 += s2 yields s1 = ";
	s1 += s2;
	cout << s1;


	cout << "\n\ns1 += \" to you\" yields\n";
	s1 += " to you";
	cout << "s1 =  " << s1 << "\n\n";


	cout << "The substring of s1 starting at \n"
		<< "location 0 for 14 characters, s1(0, 14), is:\n"
		<< s1(0, 14 ) << "\n\n";


	cout << "The substring of s1 starting at\n"
		<< "location 15, s1(15, 0), is: "
		<< s1( 15, 0 ) << "\n\n";


	String *s4Ptr = new String( s1 );
	cout << "\n*s4Ptr = " << *s4Ptr << "\n\n";


	cout << "assigning *s4Ptr to *s4Ptr\n"
		*s4Ptr = *s4Ptr;
	cout << "*s4Ptr = " << *s4Ptr << '\n';


	delete s4Ptr;


	s1[ 0 ] = 'H';
	s1[ 6 ] = 'B';
	cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "
		<< s1 << "\n\n";


	cout << "Atempt to assign 'd' to s1[30] yields:" << endl;
	s1[ 30 ] = 'd';

	return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -