立即学习:https://edu.csdn.net/course/play/9186/191693?utm_source=blogtoedu
// project8.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
using namespace std;
struct conf
{
char itemname[40];
char itemcontent[100];
};
char* getinfo(vector<conf*>& conflist,const char* pitem)
{
for (auto pos = conflist.begin(); pos != conflist.end(); ++pos)
{
if (_stricmp((*pos)->itemname, pitem) == 0)
{
return (*pos)->itemcontent;
}
}
return nullptr;
}
int main()
{
//SeverName = 1区; // 表示服务器名称;
// SeverID = 100000; // 表示服务器ID;
conf* pconf1 = new conf;
strcpy_s(pconf1->itemname, sizeof(pconf1->itemname), "SeverName");
strcpy_s(pconf1->itemcontent, sizeof(pconf1->itemcontent), "1区");
conf* pconf2 = new conf;
strcpy_s(pconf2->itemname, sizeof(pconf2->itemname), "SeverID");
strcpy_s(pconf2->itemcontent, sizeof(pconf2->itemcontent), "100000");
vector<conf*> conflist;
conflist.push_back(pconf1); //[0]
conflist.push_back(pconf2);//[1]
char* p_tmp = getinfo(conflist, "SeverName");
if (p_tmp != nullptr)
{
cout << p_tmp << endl;
}
//我们要释放内存
std::vector<conf*>::iterator pos;
for (pos = conflist.begin(); pos != conflist.end(); ++pos)
{
delete(*pos); //*pos才是那个值
}
conflist.clear();
//vector<student> sv;
//student mystu;
//mystu.num = 100;
//sv.push_back(mystu); // 把对象mystu赋值到了sv容器中;
//vector<student>::iterator iter;
//iter = sv.begin(); //指向第一个元素
//cout << (*iter).num << endl;
//cout << iter->num << endl;
/*const vector<int> iv = { 100,200,300 };*/
/*vector<int> iv = { 100, 200, 300 };*/
//vector<int>::const_iterator iter;
//for (iter = iv.begin(); iter != iv.end(); ++iter)
//{
// //*iter = 4; //报错
// cout << *iter << endl;
//}
//5.1 cbegin和cend
//for (auto iter = iv.cbegin(); iter != iv.cend(); ++iter)
//{
// cout << *iter << endl;
//}
//迭代器失效
//vector<int> vecvalue{ 1,2,3,4,5 };
/*for (auto vecitm : vecvalue)
{
vecvalue.push_back(888);
cout << vecitm << endl;
}*/
/*for (auto beg = vecvalue.begin(), end = vecvalue.end(); beg != end; ++beg)
{
vecvalue.push_back(888);
cout << *beg << endl;
}*/
//改进
//
//迭代器失效(灾难程序崩溃)
//vector<int>vecvalue = {1,2,3,4,5};
//auto beg = vecvalue.begin();
//auto end = vecvalue.end();
//while (beg != end)
//{
// cout << *beg << endl;
// vecvalue.insert(beg, 80);
// break;
// ++beg;
//}
//beg = vecvalue.begin();
//end = vecvalue.end();
//while (beg != end)
//{
// cout << *beg << endl;
// ++beg;
//}
//auto beg = vecvalue.begin();
//int icount = 0;
//while (beg != vecvalue.end()) //随时更新end
//{
// beg = vecvalue.insert(beg, icount +80); //insert的返回结果就要接着
// icount++;
// if (icount > 10)
// break;
// ++beg;
//}
//
// beg = vecvalue.begin();
//auto end = vecvalue.end();
//
//while (beg != end) //随时更新end
//{
//
// cout << *beg << endl;
// ++beg; //不要忘记放在末尾
//}
//正确方法演示
//vector<int> iv = {100,200,300};
//while (!iv.empty())
//{
// auto iter = iv.begin(); //因为不为空,所以返回begin()是没有问题的
// iv.erase(iter); //删除该位置上的元素;
//}
//范例演示: 用迭代器遍历一下string类型数据
/*string str("I Love China!");*/
/* for (auto iter = str.begin(); iter != str.end(); ++iter)
{
*iter = toupper(*iter);
}
cout << str << endl;*/
return 0;
}