📄 stltest.cpp
字号:
#include "STLTest.h"
vector<int> intVector(100);
struct STL_Group
{
CString Sname;
int iNUm;
char * NUm;
};
int Test_Remove_if()
{
vector<CString> vs(10,"12345哈哈");
FindStr fs;
fs.iMode = FM_IS;
fs.szMatchStr = "哈哈";
for(int i=0;i!=vs.size();++i)
{
cout << "前Test_Remove_if():" << vs[i] << endl;
}
vs.erase(std::remove_if(vs.begin(), vs.end(), FindMatchingString(&fs)), vs.end());
for(int i=0;i!=vs.size();++i)
{
cout << "后Test_Remove_if():" << vs[i] << endl;
}
return 0;
}
void Test_ZipVector()
{
vector<CString> v(1000);
vector<CString> vNew(v);
cout << v.size();
for(int i=0;i!=7;i++)
{
v[i]="ss";
}
cout << vNew.capacity();
vector<CString>(vNew).swap(v);
}
void Test_StdAt(int iAt)
{
vector< int > ivec;
ivec.reserve(20);
for(int i=0;i!=12;i++)
ivec.push_back(i);
try
{
cout << ivec.at(iAt) << endl;
cout << ivec.operator [](iAt) ;
}
catch (const exception& e)
{
cout << e.what();
}
}
void Test_Swap()
{
std::vector<int> v;
std::vector<int> v1(10,7);
v.reserve(100);
for(int i=0;i!=v1.size();i++)
{
cout << "Test_Swap():" << v1[i] << endl;
}
v.push_back(111);
v.push_back(222);
v.push_back(333);
v.push_back(444);
v.push_back(555);
v.push_back(666);
v.push_back(777);
v1.swap(v);
for(int i=0;i!=v1.size();i++)
{
cout << "Test_Swap():" << v1[i] << endl;
}
//试验
std::vector<int> _Tlist;
_Tlist.reserve(10);
for(int i = 0; i < 10; ++i){
_Tlist.push_back(i);
}
for(int i=0;i!=_Tlist.size();i++)
{
cout << "resize前Test_Swap():" << _Tlist[i] << endl;
}
// do something
_Tlist.resize(0);
for(int i = 0; i < 20; ++i){
_Tlist.push_back(i);
}
for(int i=0;i!=_Tlist.size();i++)
{
cout << "resize后Test_Swap():" << _Tlist[i] << endl;
}
system("pause");
}
int main()
{
vector<STL_Group> Svec;
STL_Group TempGroup;
int iTemp;
vector<int> v;
// v.reserve(10);
char a[] = "hello world";
char *pi = a;
cout<< sizeof(a) << endl; // 12字节
cout<< sizeof(pi) << endl; // 4字节
a[0] = 'X';
cout << a << endl;
system("pause");
char *p = "world";
p[0] = 'X'; //无效
cout << "char[size]:" << sizeof(char) << endl;
cout << "int[size]:" << sizeof(int) << endl;
cout << "unsigned int[size]:" << sizeof(unsigned int) << endl;
cout << "long[size]:" << sizeof(long) << endl;
cout << "unsigned long[size]:" << sizeof(unsigned long) << endl;
cout << "float[size]:" << sizeof(float) << endl;
cout << "double[size]:" << sizeof(double) << endl;
cout << "void *[size]:" << sizeof(void *) << endl;
cout << p << endl;
system("pause");
Test_Swap();
//高效拷贝文件一个一个字符串到fileData中
//ifstream inputFile("interestingData.txt");
//string fileData((istreambuf_iterator<char>(inputFile)),
// istreambuf_iterator<char>());
cout << v.size() << endl;
for(int i=0; i<100; i++)
{
v.push_back(i);
}
v.push_back(2);
v.push_back(12);
v.push_back(222);
v.push_back(15);
cout << v.size() << endl;
//测试删除
if(v.size()>=83)
v.erase(v.begin(), v.begin()+83);
else
cout << "超界!!" << endl;
if(v.size()>=21
)
{
for(int i=0; i<21; i++)
{
cout << "元素内容:" << v.at(i) << endl;
}
}
cout << v.size() << endl;
system("pause");
for(int i=0; i<100; i++)
{
v.push_back(i);
}
cout << v.size() << endl;
system("pause");
cout << "v的max_size:" << v.max_size() << "v1的值:"<< v[2] << endl ;
v.reserve(122);
cout << "v的max_size:" << v.max_size() << "v1的值:"<< v[2] << endl ;
cout << v.size() << endl;
cout << "测试at函数: " ;
cin >> iTemp;
Test_StdAt(iTemp);
system("pause");
Test_Remove_if();
system("pause");
Test_ZipVector();
system("pause");
system("pause");
TempGroup.iNUm=1;
TempGroup.NUm="";
// TempGroup.Sname
// Svec.push_back();
intVector[21] = 50;
vector<int>::iterator intIter = find(intVector.begin(), intVector.end(), 50);
if (intIter != intVector.end())
cout << "Vector contains value " << *intIter << endl;
else
cout << "Vector does not contain 50" << endl;
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -