12.9-02

1.strcat_s(*des,len1+len2,*src);

第二个参数是合并字符串后的字符数量。 即,源串大小 + 目标串大小 + 字符串结束符大小("\0")。

注意第二个参数是缓冲区的总大小,而不是剩余的大小:
#include <stdlib.h> // malloc()  
#include <string.h> // strcat_s() && strlen()  
 
int _tmain(int argc, _TCHAR* argv[])  
{  
	char *ret = (char*)malloc(120);  
	memset(ret, 0, sizeof(ret));  
	char *str1 = "This is a demonstration program, ";  
	char *str2 = "considerations for using strcat_s.";  
 
	int len1 = strlen(str1) + 1;  
	strcat_s(ret, len1, str1);  
	//strcat_s(ret, sizeof(ret), str1);      // Debug Assertion Failed  
	//strcat_s(ret, strlen(str1), str1);     // Program: ...  
	// File: .\tcscat_s.inl  
	// Line: 42  
	// Expression: (L"Buffer is too small" && 0)  
	strcat_s(ret, strlen(str1) + 1, str1);         
	int len2 = strlen(ret) + strlen(str2) + 1;         
	strcat_s(ret, len2, str2);         
	printf("%s", ret);  
 
	return 0;  
}

添加链接描述

2.必要的char *temp = new char[total_len + 1];否则中断

3.strcpy_s(temp, len + 1, str);
strcpy_s(temp + len, s.len + 1, s.str);指针的移动!!!

 String String::operator+(String&s)
 {
	/* 析构时候,发生中断
	int len = strlen(this->str) + strlen(s.str) ;
	 strcat_s(this->str, len + 1, s.str);
	 return this->str;*/
	 int total_len = len + s.len;
	 char *temp = new char[total_len + 1];
	 strcpy_s(temp, len + 1, str);
	 strcpy_s(temp + len, s.len + 1, s.str);
	 temp[total_len] = '\0';
	 return String(temp);
 }

4.必要的:String & String::operator=(const String & st);如果没有
将执行String::String(const char *s)!!!发生中断

	s2 = "My name is " + s3;
 String & String::operator=(const String & st)
 {
	 if (this == &st)
		 return *this;
	 delete[] str;
	 len = st.len;
	 str = new char[len + 1];
	 strcpy_s(str, std::strlen(st.str) + 1, st.str);
	 return *this;
 }

我想是 差一个delete[] str;打印乱码!

String::String(const char *s)
{
	len = strlen(s);
	str = new char[len + 1];
	memset(str, 0, sizeof(str));
	strcpy_s(str, strlen(s)+1, s);
}

5.不需要重载[],类似初始化数组int p[3]={1,2,3}!

String rgb[3] = { String(s1), String("green"), String("blue") };//不需要重载[],类似初始化数组int p[3]={1,2,3}!
cout << rgb[1] << endl;

6.发生中断
在这里插入图片描述

std::istream & operator >> (std::istream & is, String &s)
{
	is >> s.str;
	return is;
}

while (cin>>ans)

7.strcmp用法

 bool  operator==(String s1, String s2)
 {
	 return (strcmp(s1.str, s2.str)==0);
 }

最终源码

#ifndef STRING2_H_
#define  STRING2_H_
#include <iostream>

class String
{
public:
	String();
	~String();
	String(const String&);
	String(const char *s);
	friend std::ostream & operator << (std::ostream & os,String &s);
	friend std::istream & operator >> (std::istream & is, String &s);
	//错误:friend char* operator+(char*, String&);
	friend String operator+(char*, String&);
	String& operator= (char*);
	//String& operator+(String&);//返回值不能是引用,因为返回是一个新的
	String operator+(String&);
	String & operator=(const String & );
	void stringup();
	int has(char s);
	//char& operator [](int i);
	void stringlow();
	bool friend operator==(String s1, String s2);
protected:
private:
	char*str;
	int len;
	static const int CINLIM = 80;
};

#endif STRING2_H_

#include "String2.h"

String::String()
{
	str = new char[1];
	str = '\0';
	len = 0;
}
String::~String()
{
	delete[] str;
}
String::String(const String&s)
{
	len = s.len;
	str = new char[strlen(s.str) + 1];
	strcpy_s(str, strlen(s.str)+1, s.str);
}
String::String(const char *s)
{
	len = strlen(s);
	str = new char[len + 1];
	memset(str, 0, sizeof(str));
	strcpy_s(str, strlen(s)+1, s);
}

std::ostream & operator << (std::ostream & os, String &s)
{
	os << s.str;
	return os;
}
/*当while (cin>>ans)时,发生中断:pointer is null
std::istream & operator >> (std::istream & is, String &s)
{
	is >> s.str;
	return is;
}*/
std::istream & operator >> (std::istream & is, String &s)
{
	char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is)
	{
		s = temp;//char temp[80]==>" 一串字符串"==>String::String(const char *s)
	} 
	while (is&&is.get()!='\n')
	{
		continue;
	}
	return is;
}

String operator + (char*s, String&s1)
{
	return String(s) + s1;
}
 String& String::operator= (char*s)
 {
	 delete[]str;
	 len = strlen(s);
	 str = new char[len + 1];
	 strcpy_s(str, strlen(s)+1, s);
	 return *this;
 }
 String String::operator+(String&s)
 {
	 /*int len = strlen(this->str) + strlen(s.str) ;
	 strcat_s(this->str, len + 1, s.str);
	 return String(this->str);*/
	 int total_len = len + s.len;
	 char *temp = new char[total_len + 1];
	 strcpy_s(temp, len + 1, str);
	 strcpy_s(temp + len, s.len + 1, s.str);
	 temp[total_len] = '\0';
	 return String(temp);
 }


 String & String::operator=(const String & st)
 {
	 if (this == &st)
		 return *this;
	 delete[] str;
	 len = st.len;
	 str = new char[len + 1];
	 strcpy_s(str, std::strlen(st.str) + 1, st.str);
	 return *this;
 }

 void String::stringup()
 {
	 int i = 0;
	 for (i = 0; i < strlen(str);i++)
	 {
		 if (str[i]>='a'&&str[i]<='z'&&str[i]!=32)
		 {
			 str[i] = this->str[i] - 32;//1.不是'32'
		 }
			continue;
	 }
 }
 int String::has(char s)
 {
	 int i = 0;
	 int count = 0;
	 for (i = 0; i < strlen(this->str);i++)
	 {
		 if (str[i] == s)
		 {
			 count++;
		 }
	 }
	 return count;
 }
/*
 char& String::operator[](int i)
 {
	 return this->str[i];
 }*/

 void String::stringlow()
 {
	 for (int i = 0; i < len + 1;i++)
	 {
		 str[i] = tolower(this->str[i]);
	 }
 }

 bool  operator==(String s1, String s2)
 {
	 return (strcmp(s1.str, s2.str)==0);
 }

#include <iostream>
#include "string2.h"
using namespace std;

void main()
{

	String s1(" and I am a C++ student.");

	String s2 = "please enter your :";
	String s3("and I was");
	cout << s2;
	cin >> s3;
	s2 = "My name is " + s3;
	cout << s2 << ".\n";
	s2 = s2 + s1;
	cout << s2<<".\n";

	s2.stringup();
	cout << s2 << endl;
	cout << "The string \n" << s2 << "\ncontains " << s2.has('A') << "'A' characters in it.\n";

	s1 = "red";
	cout << s1 << endl;

	String rgb[3] = { String(s1), String("green"), String("blue") };//不需要重载[],类似初始化数组int p[3]={1,2,3}!
	cout << rgb[1] << endl;
	cout << "Enter the name of a primary color for mixing light: ";
	String ans;
	bool success = false;
	while (cin>>ans)
	{
		ans.stringlow();
		for (int i = 0; i < 3;i++)
		{
			if (ans==rgb[i])//rgb[1]=String;
			{
				cout << "That's right:\n";
				success = true;
				break;
			}
		}
		if (success)
			break;
		else
		{
			cout << "Try again:\n";
		}
	}
	cout<<"Bye.."<<endl;
	system("pause");
	return ;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值