📄 stl里面的vector疑惑.cpp
字号:
// STL里面的vector疑惑.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Tclass
{
private:
char *pChar;
public:
Tclass() //默认的构造函数
{
pChar = NULL;
}
Tclass(char *_pChar,int length);
void display();
~Tclass();
};
Tclass::Tclass(char *_pChar,int length)
{
if ( _pChar == NULL )
{
cout << "字符指针不能为空!"<<endl;
return; //不应该有返回
}
else
{
pChar = new char[length+1];
memset(pChar,0,length+1);
memcpy(pChar,_pChar,length);
}
}
Tclass::~Tclass()
{
if (pChar !=NULL)
delete[] pChar;
};
void Tclass::display()
{
cout<<pChar<<endl;
}
void main()
{
vector<Tclass > v_Tclass;
v_Tclass.clear();
for (int i =0 ;i<4; i++)
{
v_Tclass.push_back(Tclass("test",4)); //"test"占五个字符,可是之分配了四个空间
}
vector<Tclass>::iterator p;
//dispaly all
for( p = v_Tclass.begin();p!=v_Tclass.end();p++)
{
p->display();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -