1.list容器
#include <iostream>
#include <list>
using namespace std;
void list_show(list<int> l)
{
list<int>::iterator i;
for(i=l.begin();i!=l.end();i++)
{
cout << "----------" << " " << *i << " " << "----------" << endl;
}
}
void three_attributes(list<int> l)
{
cout << "----------" << "容器判空 " << l.empty() << "----------" << endl;//1为空,0为非空
cout << "----------" << "容器最大容量 " << l.max_size() << "----------" << endl;
cout << "----------" << "容器当前容量 " << l.size() << "----------" << endl;
}
int main()
{
cout << "----------" << " 创建一个空的list " << "----------" << endl;
list<int> l1;
three_attributes(l1);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*给list1赋值3个以6为值的元素*/
l1.assign(3,6);//第二种,第一种的迭代器加法报错
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*删除list1中一个元素(始终删除的是list中第一个元素)*/
l1.erase(l1.begin());//第一种,第二种的迭代器加法报错
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*给list1赋值3个以9为值的元素*/
l1.assign(3,9);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*清空list1中的所有元素*/
l1.clear();
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*给list1赋值3个以9为值的元素*/
l1.assign(3,9);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*从第一个元素的位置向list1插入3个以6为值的元素*/
l1.insert(l1.begin(),3,6);//第二种,第三种的迭代器加法报错
/*从第一个元素的位置向list1插入以2为值的元素*/
l1.insert(l1.begin(),2);//第一种
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*删除list1中重复的元素*/
l1.unique();
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*返回list1中第一个元素*/
cout << "----------" << "第一个元素 " << l1.front() << "----------" << endl;
/*返回list1中最后一个元素*/
cout << "----------" << "最后一个元素 " << l1.back() << "----------" << endl;
/*删除list1中的第一个元素*/
l1.pop_front();
/*删除list1中的最后一个元素*/
l1.pop_back();
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*从第一个元素的位置向list1插入以1为值的元素*/
l1.push_front(1);
l1.push_front(7);
/*从最后一个元素的位置向list1插入以8为值的元素*/
l1.push_back(8);
l1.push_back(7);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*将list1中的元素按升序排序*/
l1.sort();
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*删除list1中的指定值*/
l1.remove(7);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*将list1中的元素翻转*/
l1.reverse();
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*重设list1的大小,超出部分插入指定的值*/
l1.resize(5,4);
three_attributes(l1);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
/*返回list1的配置器*/
l1.get_allocator();
cout << "----------" << " 创建一个空的list " << "----------" << endl;
list<int> l2;
three_attributes(l2);
cout << "----------" << " 输出list2 " << "----------" << endl;
list_show(l2);
/*在list2的末尾插入一个元素*/
l2.push_back(7);
l2.push_back(3);
l2.push_back(2);
cout << "----------" << " 输出list2 " << "----------" << endl;
list_show(l2);
/*交换list2和list1*/
l2.swap(l1);
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
cout << "----------" << " 输出list2 " << "----------" << endl;
list_show(l2);
// /*将list2合并到list1中*/
// l1.merge(l2);//第一种,第二种不会用
// cout << "----------" << " 输出list1 " << "----------" << endl;
// list_show(l1);
/*从list1中的第一个元素的位置开始将list2中的所有个元素合并到list1中*/
l1.splice(l1.begin(),l2);//第一种
cout << "----------" << " 输出list1 " << "----------" << endl;
list_show(l1);
// /*从list1中的第一个元素的位置开始将list2中的第一个元素合并到list1中*/
// l1.splice(l1.begin(),l2,l2.begin());//第二种,第三种的迭代器加法报错
// cout << "----------" << " 输出list1 " << "----------" << endl;
// list_show(l1);
/*rbegin、rend、remove_if不会用*/
return 0;
}
2.实现一个类,类中实现一个set函数,设置-一个成员a的值。实现Sum函数,打印1~成员a的值之间所有数字之和,实现Porduct函数,打印1~a的值之间所有数字的乘积,实现函数PrimeNumber,输出1~a的值之间的所有质数。
正常实现
#include <iostream>
using namespace std;
class Person
{
int a;
public:
Person():a(0){}
int set(int a);
int sum(void);
int mul(void);
void PrimeNumber(void);
};
int Person::set(int a)
{
this->a=a;
return this->a;
}
int Person::sum(void)
{
int sum;
sum=(1+this->a)*(this->a)/2
return sum;
}
int Person::mul(void)
{
int i,mul=1;
for(i=1;i<=this->a;i++)
{
mul=mul*i;
}
return mul;
}
void Person::PrimeNumber(void)
{
int i,j,flag=0;
for(i=1;i<=this->a;i++)
{
for(j=1;j<=i;j++)
{
if((i%j)==0)
{
flag++;
}
}
if(flag==2)
{
cout << i << "\t";
}
flag=0;
}
cout << " " << endl;
}
int main()
{
Person p1;
int n;
cin >> n;
cout << "a=" << p1.set(n) << endl;
cout << "sum=" << p1.sum() << endl;
cout << "a!=" << p1.mul() << endl;
cout << "a以内的所有质数是:";
p1.PrimeNumber();
return 0;
}
list容器实现
#include <iostream>
#include <list>
using namespace std;
class Person
{
int a;
list<int> l1;
public:
Person():a(0){}
int set(int a);
int sum(void);
int mul(void);
void PrimeNumber(void);
};
int Person::set(int a)
{
this->a=a;
for(int i=1;i<=this->a;i++)
{
l1.push_back(i);
}
return l1.back();
}
int Person::sum(void)
{
int sum;
sum=((*l1.begin())+(*l1.end()))*l1.back()/2;
return sum;
}
int Person::mul(void)
{
list<int>::iterator i;
int mul=*(l1.begin());
for(i=l1.begin();i!=l1.end();i++)
{
mul=mul*(*i);
}
return mul;
}
void Person::PrimeNumber(void)
{
list<int>::iterator i;
int j,flag=0;
for(i=l1.begin();i!=l1.end();i++)
{
for(j=1;j<=(*i);j++)
{
if(((*i)%j)==0)
{
flag++;
}
}
if(flag==2)
{
cout << *i << "\t";
}
flag=0;
}
cout << " " << endl;
}
int main()
{
Person p1;
int n;
cin >> n;
cout << "a=" << p1.set(n) << endl;
cout << "sum=" << p1.sum() << endl;
cout << "a!=" << p1.mul() << endl;
cout << "a以内的所有质数是:";
p1.PrimeNumber();
return 0;
}
3. 已知C风格的字符串,完成对字符串通过下标访问时的异常处理机制(越界访问)
#include <iostream>
#include <cstring>
using namespace std;
class A
{
char* str;
int len;
public:
A():str(new char(0)),len(0){}
A(const char* src):str(new char[strlen(src)+1]),len(strlen(src)){strcpy(str,src);}
A(const A &other):str(new char[other.len+1]),len(other.len){strcpy(str,other.str);}
A &operator=(const A &other)
{
if(&other!=this)
{
delete []this->str;
str=new char[other.len+1];
strcpy(this->str,other.str);
this->len=other.len;
}
return *this;
}
~A(){delete [] str;}
//下标访问字符串
int at(int pos)
{
if(pos<0||pos>len-1)
{
throw int(1);
return pos;
}
cout<< "-------------" << *(str+pos) << endl;
return pos;
}
//字符串长度
int size()
{
return len;
}
char* c_str()
{
return str;
}
};
int main()
{
A a1("please speak chinese");
cout << "-------------" << a1.c_str() << endl;
cout << "-------------" << "字符串长度 " << a1.size() << endl;
try
{
a1.at(20);
}
catch (int ret)
{
if(ret==1)
{
cout << "Access subscript beyond string boundary" << endl;
}
}
return 0;
}
4.写一个程序,程序包含两个类,类中实现一个成员函数,MyGetChar(), 类A中每调用一次,按顺序得到一个数字字符,比如第一次调用得到'0', 第二次得到'1',以此类推,类B没调用一次得到一个小写字母字符,比如第一次调用得到a', 第二次得到b',以此类推,程序通过交替调用类A和类B的函数,实现运行结果输出一串字符串“0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9tuvwxyz”
#include <iostream>
using namespace std;
class A
{
char a;
public:
A():a('0'){}
void MyGetChar(void);
};
class B
{
char b;
public:
B():b('a'){}
void MyGetChar(void);
};
void A::MyGetChar(void)
{
cout << this->a;
if(this->a=='9')
{
this->a='0';
return;
}
this->a++;
}
void B::MyGetChar(void)
{
cout << this->b;
if(this->b=='z')
{
this->b='a';
return;
}
this->b++;
}
int main()
{
A a1;
B b1;
int i=1;
while(i<=20)
{
a1.MyGetChar();
b1.MyGetChar();
i++;
}
i=1;
while(i<=6)
{
b1.MyGetChar();
i++;
}
cout << " " << endl;
return 0;
}
5.写一个程序,实现两个类,分别存放输入的字符串中的数字和字母,并按各自的顺序排列,类中实现一个dump函数,调用后输出类中当前存放的字符串结果。例如,输入1u4y2a3d,输出:存放字母的类,输出aduy,存放数字的类输出1234
#include <iostream>
#include <list>
using namespace std;
class A
{
list<char> l1;
public:
void Getchar(const string s);
void dump(void);
};
class B
{
list<char> l1;
public:
void Getchar(const string s);
void dump(void);
};
void A::Getchar(const string s)
{
int i=0;
int len=s.size();
while(i<len)
{
if(s.at(i)>='0'&&s.at(i)<='9')
{
l1.push_back(s.at(i));
}
i++;
}
}
void A::dump(void)
{
l1.sort();
list<char>::iterator i;
for(i=l1.begin();i!=l1.end();i++)
{
cout << *i;
}
cout << " " << endl;
}
void B::Getchar(const string s)
{
int i=0;
int len=s.size();
while(i<len)
{
if(s.at(i)>='a'&&s.at(i)<='z')
{
l1.push_back(s.at(i));
}
i++;
}
}
void B::dump(void)
{
l1.sort();
list<char>::iterator i;
for(i=l1.begin();i!=l1.end();i++)
{
cout << *i;
}
cout << " " << endl;
}
int main()
{
A a1;
B b1;
string s1;
cin >> s1;
a1.Getchar(s1);
a1.dump();
b1.Getchar(s1);
b1.dump();
return 0;
}