-
C++第七天
今天主要讲vector类模板和迭代器,以及软件qt的大概使用方法。今天代码的意思不难理解,就是自己写不出来,尤其是今天留的两道作业,第一题是刚讲过,但是整理不出头绪。
#include
#include
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector &vec)
{
//迭代器,类似于指针
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
vector vec;
//vector最开始空间为0,不能直接通过下标进行访问
//vec[100] = 90; // X
//push_back:会自动申请空间,与string分配空间的策略一样
vec.push_back(89);
cout << "size:" << vec.size() <<" capacity:"<
#include
#include
using namespace std;
class Student{};
typedef vector STUVEC;
int main(void)
{
STUVEC vec;
vec.push_back(new Student);
vec.push_back(new Student);
vec.push_back(new Student);
STUVEC::iterator iter = vec.begin();
return 0;
}
#include
#include
using namespace std;
class Student
{
public:
Student(string name="", float score=0)
{
cout << "Student(string,float)\n";
}
Student(const Student &other)
{
cout << "Student(const Student &)\n";
}
~Student(){cout << "~Student()\n";}
private:
string m_strName;
float m_fScore;
};
int main()
{
vector vec;
Student aa("aa", 11);
vec.push_back(aa);
Student bb("bb", 22);
vec.push_back(bb);
Student cc("cc", 33);
vec.push_back(cc);
Student dd("dd", 44);
vec.push_back(dd);
Student ee("ee", 55);
vec.push_back(ee);
Student ff("ff", 66);
vec.push_back(ff);
Student gg("gg", 77);
vec.push_back(gg);
cout << "Hello World!" << endl;
return 0;
}
#include
#include
using namespace std;
class Student
{
public:
Student(string name="")
{
m_strName = name;
}
const string &getName(){return m_strName;}
private:
string m_strName;
};
int main(void)
{
//vector vec;
//vector vec[1024];
string strName;
vector > vec;
for (int i = 0; i < 3; i++)
{
vector tmp;
for (int j = 0; j < 2; j++)
{
cout << "input name:";
cin >> strName;
tmp.push_back(new Student(strName));
}
vec.push_back(tmp);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << vec[i][j]->getName() << ' ';
}
cout << '\n';
}
return 0;
}
#include
#include
using namespace std;
class Student
{
public:
Student(string name="", float score=0)
{
cout << "Student(string,float)\n";
}
Student(const Student &other)
{
cout << "Student(const Student &)\n";
}
~Student(){cout << "~Student()\n";}
private:
string m_strName;
float m_fScore;
};
int main()
{
vector vec;
vec.push_back(new Student("aa", 11));
vec.push_back(new Student("bb", 22));
vec.push_back(new Student("cc", 33));
vec.push_back(new Student("dd", 44));
vec.push_back(new Student("ee", 55));
vec.push_back(new Student("ff", 66));
vec.push_back(new Student("gg", 77));
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
delete (*iter);
}
vec.clear();
cout << "Hello World!" << endl;
return 0;
}
#include
#include
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector &vec)
{
//迭代器,类似于指针
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
//vector vec(32, 888);
vector vec(32);
vec.clear();
cout << vec.size() << ' ' << vec.capacity() << endl;
print(vec);
cout << "Hello World!" << endl;
return 0;
}
#include
#include
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector &vec)
{
//迭代器,类似于指针
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
int a[7] = {19, 67, 89, 3, 16, 22, 15};
vector vec(a, a+7); //[)
print(vec);
//vec.resize(3);
//vec.resize(13);
vec.resize(13, 888);
print(vec);
#if 0
vec.assign(3, 888);
print(vec);
#endif
cout << vec.size() << ' ' << vec.capacity() << endl;
cout << "Hello World!" << endl;
return 0;
}
#include
#include
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector &vec)
{
//迭代器,类似于指针
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
vector vec;
vec.reserve(1024);
cout << "size:" << vec.size() << " capacity:"<
#include
#include
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector &vec)
{
//迭代器,类似于指针
vector::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
int a[7] = {19, 67, 89, 3, 16, 22, 15};
vector vec(a, a+7); //[)
print(vec);
vector::iterator iter = vec.begin();
iter = iter+3;
vec.insert(iter, 888);
print(vec);
cout << "----------------\n";
iter = vec.begin();
for (;iter != vec.end();)
{
if (0 != (*iter)%2)
{
//对容器进行增删后,所有迭代器失效
//必须对迭代器进行重新赋值
//erase返回值为下一个元素的迭代器
iter = vec.erase(iter);//对容器进行删除
}
else
{
iter++;
}
}
print(vec);
cout << "Hello World!" << endl;
return 0;
}
作业:
1、a.动态生成多个学生对象,将学生对象的指针放入容器中,然后将容器中的数据写入文件中
b.将文件中的学生数据读取出来放入容器中并显示
2、写一个类模板,用来对不同类型的数据进行排序