一、字符串赋值
String::String(const char *c) {
for (int i = 0; c[i] != '\0'; i++) {
str[i] = c[i];
}
str[strlen(c)] = '\0';
}
把一个字符串赋值给另一个字符串
str[strlen(c)] = '\0';
另一种写法
正确代码
String::String(const char *c) { size = strlen(c); // 获取字符数组的长度 str = new char[size + 1]; // 为str分配内存 for(int i = 0; i < size; i++) { str[i] = c[i]; } str[size] = '\0'; }
strlen没有算'\0'
步骤:计算传进来的字符串的大小,为被赋值字符串分配内存,for循环实现赋值,最后为str[size],字符串的第size个赋值为'\0'
二、输入到特殊的值时循环停止(用while循环)
三、对动态数组排序
引进来局部变量tmp;
template<class T>
void Vector<T>::sortv()
{
for(int i=0;i<=size-1;i++)
{
for(int j=i+1;j<size;j++)
{
if (data[j]<data[i])
{
T tmp = data[j];
data[j]=data[i];
data[i]= tmp; // T tmp错误
}
}
}
}
template<class T>
void Vector<T>::sortv()
{
for(int i=0;i<=size-1;i++)
{
for(int j=i+1;j<size;j++)
{
if (data[j]<data[i])
{
T tmp = data[j];
data[j]=data[i];
data[i]= tmp;
}
}
}
}
int main(void)
{
Vector<int> v;
int i = 1;
while(i != 0)
{
cin>>i;
v.Push_back(i);
}
v.sortv();
for(Vector<int>::iterator it = v.begin(); it!=v.end(); it++)
{
cout<<*it<<endl;
}
return 0;
}
三、
L.Erase((L.End()-2) //填空4 删除倒数第二个 并显示剩余的元素
display_seqlist(L.Begin(),L.End());
L.End(); 指的是最后元素的后面;
L.Erase(L.End()-2)
四、
strcpy的用法
五、
类内字符数组:char Name[20]
strcpy(a.b) //把b字符串赋值给a
#include<iostream>
using namespace std;
#include<cstring>
class Vehicle
{
public:
virtual void display()=0;
char Name[20];
};
class Car:public Vehicle
{
public:
void display()
{
totalprice=distance*price;
cout<<Name<<"应收费"<<totalprice<<"元"<<endl;
}
Car(char *s,int d)
{
strcpy(Name,s);
distance=d;
}
private:
int distance;
float price = 0.4;
float totalprice;
};
int main(void)
{
char s[20]={"奔驰"};
Car a(s,500);
a.display();
}
六、字符串大小比较/操作符“+=”重载(返回自身,用引用)
String& String::operator +=(const String &a)
{
*this = *this+"__";
*this = *this +a;
return *this;
}
bool String::operator<(const String &b)
{
return(strcmp(str,b.str)<0);
}
在C++中,返回引用和加const有以下几个原因:
返回引用:返回引用可以避免对象的拷贝,提高效率。在这个代码片段中,返回的是一个指向当前对象的引用(*this),这样可以直接在赋值操作完成后返回自身。
加const:加const修饰函数的参数或返回值可以保证函数不会修改传入的对象或返回的对象,增加代码的可读性和安全性。在这个代码片段中,参数a被加上了const修饰符,表示不会对传入的对象进行修改。
关于上述代码片段中的operator=函数,其作用是实现赋值操作。具体来说:
*this = *this "__";
这一行代码使用operator=函数将当前对象的内容修改为“__”。
*this = *this+ a;
这一行代码使用operator=函数将当前对象的内容修改为参数a所代表的对象的内容。
return *this;
这一行代码返回了一个指向当前对象的引用。
作用域紧挨着oeprator
bool String::operator<(const String &b)
{
return(strcmp(str,b.str)<0);
}
strcmp函数用法
strcmp函数详解看这一篇就够了-C语言(函数实现、使用用法举例、作用、与strncmp的区别)_lambo mercy的博客-CSDN博客
函数原型:int strcmp(const char* str1, const char* str2)
头 文 件:#include <string.h>
返 回 值:str1 = str2 则返回0,
str1 > str2 则返回大于0的值,
str1 < str2 则返回小于0的值
#include<string.h>
using namespace std;
#include<iostream>
class String
{
private:
char *str;
int size;
void Error(const char* c)const; //错误信息报告
public:
//构造和析构
String(const char *c=""); //默认构造函数
String(const String& s); //复制构造
~String(); //析构
//访问
String& operator=(const char *c); //转换赋值:类串=C串
String& operator=(const String& s); //复制赋值:类串=类串
String& Insert(int id,const String& s); //子串插入
String& Erase(int id,int num); //子串删除
String SubStr(int id,int num)const; //求子串
String operator+(const String& s)const; //串连接:类串=类串+类串
String operator+(const char *c)const; //串连接:类串+C串
friend String operator+(const char *c,const String& s);//串连接:C串+类串
bool operator==(const String& s)const; //串比较:类串==类串
bool operator==(const char *c)const; //串比较:类串==C串
friend bool operator==(const char* c,const String& s);//串比较:C串==类串
operator char*()const; //成员转换
char& operator[](int id); //下标运算符
const char& operator[](int id)const;//常量型下标运算符
int Size(void)const{return(size);} //求串长
int Find_First_Of(char ch,int id)const; //字符查找
int Find_First_Of(const String& s,int id)const; //字串查找
//输入输出
friend istream& operator>>(istream& istr,String& s); //提取符
friend ostream& operator<<(ostream& ostr,const String& s);//插入符
int ReadString(istream& istr=cin,char delimiter='\n'); //串读取
// 此处为考生添加成员函数声明代码。
String& operator+=(const String &a);
bool operator<(const String &b);
//
};
// 此处为考生添加成员函数实现代码。
String& String::operator +=(const String &a)
{
*this = *this+"__";
*this = *this +a;
return *this;
}
bool String::operator<(const String &b)
{
return(strcmp(str,b.str)<0);
}
//
int main()
{
int i, n;
cin>>n;
String tmp, total, *ss = new String[n]; // 不考虑 new 失败的情况。
for(i=0; i<n; i++) cin>>ss[i];
while(n-- > 0) // 冒泡排序
{
for(i=0; i<n; i++)
{
if(ss[i] < ss[i+1])
{
tmp = ss[i];
ss[i] = ss[i+1];
ss[i+1] = tmp;
}
}
total += ss[n];
}
cout<<total<<endl;
delete []ss;
return 0;
}
七、栈输出技术
while(!S.Empty())
if(S.Top()%2!=0)
cout<<S.Pop()<<'\t';//如果是奇数,就输出
else
S.Pop(); //如果不是,就清除
return 0;
判断是否为空作为循环条件
if else 都要弹出元素,区别是奇数需要打印