标签:std pop char 数组下标 har print vector 下标 namespace
Code:
1 #include <vector> 2 using namespace std; 3 4 struct NPC 5 { 6 char * name; 7 int age; 8 float high; 9 }; 10 int main(){ 11 12 //练习使用vector数组 13 bool isKong = false; 14 NPC player = {0}; 15 NPC players[3] = { { "ralian", 18, 170.0f }, { "xer.m", 21, 167.2f }, { "1ass o", 24, 164.8f } }; 16 vector<NPC> player1(3,{ "ralian", 18, 170.0 }); 17 vector<NPC> player2(3, {"tx.m",21,167.2}); 18 vector<NPC> player3(players,players+3); 19 player = player3.front();//获取数组第一个元素 20 player = player3.back();//获取数组最后一个元素 21 isKong = player3.empty();//看数组是否为空 22 player1.swap(player2); 23 swap(player1, player2); 24 player2.clear();//清空数组 25 isKong = player2.empty(); 26 //使用迭代器遍历数组 27 vector<NPC>::iterator itor; 28 for (itor = player3.begin(); itor != player3.end(); ++itor)//注意for循环里面用分号不是逗号 29 { 30 printf_s("name=%s,age=%d,high=%f\n", itor->name, itor->age, itor->high); 31 } 32 player2.push_back({ "xiaojian", 22, 158.0f });//向尾部插入元素 33 player1.insert(player1.end(), { "xm", 53, 120.5f }); 34 player1.insert(player1.begin()+1,10,{ "soug", 33, 117.6f }); 35 player1.pop_back();//删除结尾的元素 36 player1._Pop_back_n(3);//删除结尾的最后3元素 37 player1.erase(player1.begin()+2);//删除数组下标为2的元素 38 player1.erase(player1.begin() + 1, player1.begin() + 3);//删除数组下标为[1,3)的元素 39 player1.erase(player1.begin() + 1, player1.end());//删除数组下标从1开始到最后一个元素 40 return 0; 41 }
标签:std pop char 数组下标 har print vector 下标 namespace
原文地址:https://www.cnblogs.com/mktest123/p/12172638.html