-
C++第七天
今天主要講vector類模板和迭代器,以及軟件qt的大概使用方法。今天代碼的意思不難理解,就是自己寫不出來然走,尤其是今天留的兩道作業(yè),第一題是剛講過,但是整理不出頭緒援制。
#include <iostream>
#include <vector>
using namespace std;
//vector:是一個類模板,空間連續(xù),并且空間自動增長,可以把它理解為空間自動增長的數(shù)組
void print(vector<int> &vec)
{
//迭代器芍瑞,類似于指針
vector<int>::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
vector<int> vec;
//vector最開始空間為0,不能直接通過下標進行訪問
//vec[100] = 90; // X
//push_back:會自動申請空間,與string分配空間的策略一樣
vec.push_back(89);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
vec.push_back(99);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
vec.push_back(79);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
vec.push_back(69);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
vec.push_back(89);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
vec.push_back(109);
cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
print(vec);
#if 0
for (int i=0; i < vec.size(); i++)
{
cout << vec[i] << ' ';
}
cout << endl;
#endif
cout << "Hello World!" << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class Student{};
typedef vector<Student *> 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 <iostream>
#include <vector>
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<Student> 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 <iostream>
#include <vector>
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<Student *> vec;
//vector<Student *> vec[1024];
string strName;
vector<vector<Student *> > vec;
for (int i = 0; i < 3; i++)
{
vector<Student *> 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 <iostream>
#include <vector>
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<Student*> 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<Student*>::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
delete (*iter);
}
vec.clear();
cout << "Hello World!" << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一個類模板,空間連續(xù)晨仑,并且空間自動增長,可以把它理解為空間自動增長的數(shù)組
void print(vector<int> &vec)
{
//迭代器,類似于指針
vector<int>::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
//vector<int> vec(32, 888);
vector<int> vec(32);
vec.clear();
cout << vec.size() << ' ' << vec.capacity() << endl;
print(vec);
cout << "Hello World!" << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一個類模板,空間連續(xù)拆檬,并且空間自動增長,可以把它理解為空間自動增長的數(shù)組
void print(vector<int> &vec)
{
//迭代器洪己,類似于指針
vector<int>::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<int> 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 <iostream>
#include <vector>
using namespace std;
//vector:是一個類模板,空間連續(xù),并且空間自動增長,可以把它理解為空間自動增長的數(shù)組
void print(vector<int> &vec)
{
//迭代器秩仆,類似于指針
vector<int>::iterator iter;
iter = vec.begin();
for (; iter != vec.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl;
}
int main()
{
vector<int> vec;
vec.reserve(1024);
cout << "size:" << vec.size() << " capacity:"<<vec.capacity()<<endl;
print(vec);
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一個類模板,空間連續(xù)码泛,并且空間自動增長,可以把它理解為空間自動增長的數(shù)組
void print(vector<int> &vec)
{
//迭代器,類似于指針
vector<int>::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<int> vec(a, a+7); //[)
print(vec);
vector<int>::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;
}
作業(yè):
1噪珊、a.動態(tài)生成多個學生對象,將學生對象的指針放入容器中,然后將容器中的數(shù)據(jù)寫入文件中
b.將文件中的學生數(shù)據(jù)讀取出來放入容器中并顯示
2齐莲、寫一個類模板痢站,用來對不同類型的數(shù)據(jù)進行排序