#include "project4.h" #include//stdio.h #include #include #include using namespace std; struct conf { char itemName[40]; char itemCount[100]; }; // * pitem 是char 型的指针。 & confList是confList的地址 // (*pos)->itemName 与 pitem 比较 ,(*pos)->itemName 表示是指针指向的字段变量 char* getinfo(vector & confList, const char* pitem) // 返回字符串,getinfo(vector & confList, const char* pitem)是地址 字符串在内存中是以地址的形式进行索引 { for (auto pos = confList.begin(); pos!=confList.end();++pos) { // _strcmpi 比较二者是否相等,相等则返回0.忽略大小写 // ,两边都是地址,字符串用地址的首位表示 if (_strcmpi((*pos)->itemName, pitem)==0) { return (*pos)->itemCount; } } return nullptr; } // 指针的两个例子 //int main() //{ // int a = 3; // int* b = &a; ///!!! // cout << "a:" << a << endl; //a:3 // cout << "b:" << b << endl; //b:012FFEB8 // *b = 10; // cout << "&a:" << &a << endl;//&a:012FFEB8 // cout << "b:" << b << endl; //b:012FFEB8 // cout << "a:" << a << endl; //a : 10 // system("pause"); // //分析: // //b是a的指针,指向a的地址。(也就是a与b相连,只要修改* b的值,a的值也跟着改动) //} //int main() //{ // int var = 20; // 实际变量的声明 // int* ip; // 指针变量的声明 // ip = &var; // 在指针变量中存储 var 的地址 !!! 与下面 int* ip = &var; 一样 // // //int* ip = &var; //*ip = 20 // // //int(*ip) = &var; // *ip = 20 // // //int* ip = &var; // *ip = 20 // // cout << "Value of var variable: "; // cout << var << endl; // // // 输出在指针变量中存储的地址 // cout << "Address stored in ip variable: "; // cout << ip << endl; // // // 访问指针中地址的值 // cout << "Value of *ip variable: "; // cout << *ip << endl; // // // // return 0; //} //Value of var variable : 20 //Address stored in ip variable : 0xbfc601ac //Value of * ip variable : 20 //int main() //{ // char var[20] = "这是一个测试"; // 实际变量的声明 // char* ip; // 指针变量的声明 // ip = var; // 如果是整型,则为ip = &var; 字符串 用地址表示它的值 // // // cout << "Value of var variable: "; // cout << var << endl; // // // 输出在指针变量中存储的地址 // cout << "Address stored in ip variable: "; // cout << ip << endl; // // // 访问指针中地址的值 // cout << "Value of *ip variable: "; //空 // cout << *ip << endl; // // // // return 0; //} int main() { //迭代器 vector iv = { 100,200,300 }; //vector ::iterator iter;//定义迭代器,也必须是vector 有点像指针,指向某个元素 //iter = iv.begin(); // 返回迭代器,指向iv的第一个元素iv[0] //iter = iv.end(); 指向最后一个元素 再往后一个空位置 vector iv2 = { 100,200,300 }; /// /// 传统用法 /// /////for (vector ::iterator iter = iv2.begin(); iter != iv2.end(); iter++) //{ // cout << *iter << endl; //} /// /// 反向迭代器 /// /////for (vector ::reverse_iterator riter = iv2.rbegin(); riter != iv2.rend(); riter++) //{ // cout << *riter << endl; //} //容器类名::iterator 迭代器名; 迭代器(STL迭代器)iterator vector ::iterator iter = iv.begin(); iter++; cout << *iter << endl; // 200 从100 往下迭代一个 iter--指向上一个 //const_iterator 常量指针,只能读,不能改写。删除第一个元素的方法。 while (!iv.empty()) { auto iter = iv.begin(); iv.erase(iter); //不断删除第一个元素 } //转成大写 cout << "----------------以下为转成大写-------------------------" << endl; string str("I love you!"); for (auto iter = str.begin(); iter != str.end(); ++ iter) { *iter = toupper(*iter); //转成大写 } cout << str << endl; cout << "------------------以下为指针迭代器----------------------" << endl; //先建立2个结构体,在main文件之前已经定义 conf* pconf1 = new conf; //给pconf1开辟内存空间 //strcpy_s(name1,name2.size()+1,name2.c_str()); 将name2 复制到name1 strcpy_s(pconf1->itemName, sizeof(pconf1->itemName), "ServerName"); strcpy_s(pconf1->itemCount, sizeof(pconf1->itemCount), "测试啊啊啊啊啊啊"); conf* pconf2 = new conf; //给pconf1开辟内存空间 strcpy_s(pconf2->itemName, sizeof(pconf2->itemName), "ServerID"); strcpy_s(pconf2->itemCount, sizeof(pconf2->itemCount), "10000"); vector confList; //add到指针迭代器 confList.push_back(pconf1); //[0] confList.push_back(pconf2); //[1] //cout << "---------------以下为指针迭代器-------------------------" << endl; //char* p_temp char* p_temp = getinfo(confList, "ServerName"); //getinfo返回值类型为char* 指针 if (p_temp != nullptr) { cout << p_temp << endl; //1区// p_temp是指针 指针中存放了 (*pos)->itemCount; } cout << "---------------释放内存-------------------------" << endl; std::vector ::iterator pos; for (pos=confList.begin();pos != confList.end();++pos) { delete (*pos); // *pos 才是那个指针 此时 还有数,只是内存地址无效了 } confList.clear(); //清空迭代器 不要也行 return 0; }
#include "projectStaticCast.h" #include//stdio.h #include using namespace std; using namespace lisi;//使用命名空间 命名空间函数的声明写在头文件 // // //int main() //{ // //隐式转换 // int m = 3 + 45.6; // double m1 = 3 + 45.6; // // //显式(强制) // //int k = 5 % 3.5; //报错 // // int k = 5 % (int)3.5; //报错 // // //C++有四种强制类型转换 // //a static_cast // //b dynamic_cast // //c const_cast // //d reinterpret_cast // //通用: // //强制类型转换名 (express); // // //2.1 static_cast // //double f = 100.34f; // //int i = (int)f; // //int i2 = static_cast (f); //C++风格 // // 类的转换 // //class A{}; // //class B : public A{}; // //B b; // //A a = static_cast(b); //把子类转换为父对象 // // void* 与其他类型指针之间的转换,void* : 无类型指针:可以指向任何指针类型(万能指针); // //int i = 10; // //int* p = &i; // //void* q = static_cast (p); //int* 转换为void* // //int* db = static_cast (q); //void* 转换为 int* // // //不可用于: // //a) 一般不能用于指针类型之间的转换比如int* 转double* ,float* 转double*等 // //double f = 100.0f; // //double* pf = &f; // //int* i = static_cast (pf); //报错 // // //2.1 dynamic_cast 主要用于父类与子类转换 // // //2.3 const_cast: 去除指针,或者引用const属性,可以吧const性质去掉 // // 编译时就会进行类型转换 // // //const int ai = 90; // //const int* pai = &ai; // //int* pai2 = const_cast (pai); //去掉了 const // //*pai2 = 120; //未定义行为 // //cout << ai << endl; // //cout << *pai << endl; // //cout << *pai2 << endl; // //cout << pai << endl; // //cout << pai2 << endl; // // //不建议用强制类型转换 reinterpret_cast危险。const_cast意味着设计缺陷 // // // return 0; //} // // //namespace lisi //{ // void func(int& ta, int& tb)// // { // // ta = 4; // tb = 5; // } // // void LisiTest() // { // std::cout<<"这是一个测试!n"; //相当于printf // //printf("这是一个测试!"); // } //}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)