stl使用注意事项

本文介绍了C++ STL中list和vector容器的操作技巧,包括如何在list中获取指定元素、vector中插入元素的方法,以及如何使用sort函数对结构体进行排序,并展示了如何在STL容器中存储结构体指针及释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.怎样获取STL list中的指定元素 貌似没有list.get(i)啊?
转载自: http://topic.youkuaiyun.com/u/20110324/10/14543271-4c23-4a18-999d-64be00c2070e.html
list<int> sortlist; sortlist.push_back(3); sortlist.push_back(15); sortlist.push_back(8); sortlist.push_back(7); sortlist.sort();排序过后我怎么获取第三个元素呢 ??
sortlist.get(3) 这个不是vector 不对的
难道只有用iter遍历吗
谢谢大家
回复:
1.恩,是的。计数遍历吧
list<int> sortlist; sortlist.push_back(3); sortlist.push_back(15); sortlist.push_back(8); sortlist.push_back(7); sortlist.sort(); list<int>::iterator iter = sortlist.begin(); for (int i=0; i<2; i++) { iter++; } cout << (*iter) << endl; //8 2.用遍历 自然没有问题 但是效率较低,看看有没有别的函数 也许不叫get 叫find之类的
3.list<int>::iterator iter = sortlist.begin();
advance(iter, n - 1);
效率和1楼是一样的是O(n),这是链表这种数据结构本身的特性啊
4.list容器只能遍历了,如果是vector可以直接取【2】
5.如果能get(i)..那vector以后怎么混..
6.LIST是链表结构,VECTOR才类似数组,前者优势是插入删除,想在O(1)时间内定位,还是用VECTOR吧
7.list 只有front和back。中间的元素 只能是遍历了
8.只能是遍历,不过你可以用advanced函数把循环隐藏在内部,你看起来也许就不会那么不爽了。
9.list底层是链表实现的,不能直接get,只能遍历。vector是数组,所以可以用下标直接访问。
10.用遍历吧,这个估计没有别的好办法吧
11.恕我孤陋,vector有get(i)吗?不是at(i)?
12.list只能用遍历了,因为它是用指针的,查找只能用遍历


2.关于stl vector插入元素的问题
转载自: http://topic.youkuaiyun.com/u/20110324/10/14543271-4c23-4a18-999d-64be00c2070e.html
stl中的vector,可以提供随机迭代器方便的取,但是存好像并不方便啊
比如共有元素10个,我要在3号位置插入一个元素,插入完成后变成11个.
可以用的vector成员函数有, insert
但是insert需要传的参数是iterator,并不是直接的一个索引
需要这样用
insert( iterator pos , XXXelement )
不能这样用
insert( uint nIndex , XXXelement )
那岂不是我要在3号位置插入元素,还需要用索引循环一遍,先找到在3号位置的Iterator,然后才能插入
int nMaxNum = (int)m_buffer.size(); int nCurrentIndex = 0; for (vector<int>::iterator pos = m_buffer.begain(); pos != m_buffer.end(); ++pos) { //此处的nIndex即为要插入元素的位置,3号位置 if(nCurrentIndex == nIndex) { m_buffer.insert( pos , xxxxx ) } }这样也太麻烦了吧,为何不提供个直接针对索引的insert呢, <<C++标准库>>和等等网站上都没有查到
回复:
1.额,如果插入频繁那么请用list
2.vector主要用于相对稳定的数据。
楼主参考一下这个吧: http://www.cplusplus.com/reference/stl/
很经典,也很全面,还有示例代码。
3.没有一个容器说插入和随机访问都很好的。这个本来就是一对矛盾。
4.嗯,vector中删除数据的效率相对其他容器要更弱一些。
5.各位大大,我是想问vector有没有成员函数,可以方便的直接在索引位置插入元素,而不是先要找到iterator才能插入
6.额 vector的迭代器 可以直接用算术运算,你想在哪个位置插入。直接用begin()返回的迭代器加上位移量就是了。
7.vector<int>::iterator pos = vec.begin()+3; 迭代器支持算术操作的,不用for循环那么麻烦的。
8.vector<int>::iterator pos = vec.begin() + x;
你对迭代器了解不透彻,多看看C++primer 这些都是简单问题,书中讲的很清楚~
9.vector不是你用迭代器+几就会到几的,内存会越界访问的,只有push_back或者insert才能动态扩展内存.
我建议你初始化vector(length);直接固定vector大小,或者使用数组.



3.STL sort 函数对结构体的排序
转载自: http://www.cnblogs.com/kdy71107216/archive/2009/03/04/1403382.html
C语言函数中,可以用qsort 来进行 结构体数组的排序,但是如果上升到C++中,对vector<NODE>的排序 qsort 就无能为力了。。。
不过C++ <algorithm> 函数库中的sort函数可以对 结构体vector进行排序。 前提是 要对 node 进行改造
例如 一个含有四项key 值的node 如下
struct node { string name; int medal[3]; }; 要对其以medal数组 降序排序,若 medal 数组完全相等,则以 name 的字典序排序 要调用 sort 函数对 node 进行排序,则 将node 改写为 struct node { string name; int medal[3]; bool operator <(const node& other) const { if(medal[0] > other.medal[0]) return true; if(medal[0] < other.medal[0]) return false; if(medal[1] > other.medal[1]) return true; if(medal[1] < other.medal[1]) return false; if(medal[2] > other.medal[2]) return true; if(medal[2] < other.medal[2]) return false; if(name < other.name) return true; return false; } };例如在全局声明了一个vector<node> V;
则调用sort 如下 sort(V.begin(),V.end());


4.如何在STL中存储结构体指针,及释放
转载自: http://4develop.in/page/Cpp/20110811_11_30d22ff0-3a95-46fe-9de3-28a0e623a931/如何在STL中存储结构体指针及释放.html
1.修改后,代码如下:
#include <iostream> #include <list> #include <numeric> #include <algorithm> #include <cstdlib> using namespace std; typedef struct STR_TYPE { char name[32]; int num; double price; }STR_ALL, *PSTR_ALL; typedef list<PSTR_ALL> LISTSTRUCT; int main(void) { STR_ALL *stu_type = new STR_ALL; memset( stu_type, 0, sizeof(STR_ALL) ); //声明i为迭代器 LISTSTRUCT::iterator iii; LISTSTRUCT listStu; stu_type->num = 1; listStu.push_back (stu_type); cout<<"listOne.begin()--- listOne.end():"<<endl; //for (iii = listp.begin(); listp.empty(); iii++) iii = listStu.begin(); PSTR_ALL stu; stu = *iii; printf( "%d\n", stu->num ); getchar(); getchar(); cout << endl; /* //使用STL的accumulate(累加)算法 int result = accumulate(listOne.begin(), listOne.end(),0); cout<<"Sum="<<result<<endl; cout<<"------------------"<<endl;*/ }主要是概念得认清,因为list中存放的是STR_ALL的指针类型,即PSTR_ALL。而使用iterator时,iterator相当于一个指针,指向list中的成员。因此,我们可以断定iterator是指向PSTR_ALL的指针,即STR_ALL的二级指针,所以,stu = *iii;在iii之前,必须使用*获得一级指针。
2.LZ把分给我吧
#include <iostream> #include <list> #include <numeric> #include <algorithm> #include <stdlib.h> using namespace std; struct STR_TYPE { char name[32]; int num; double price; }; list<STR_TYPE*> L; void Insert(const char* name, int num, double price) { STR_TYPE* pElement = new STR_TYPE; strcpy(pElement->name, name); pElement->num = num; pElement->price = price; L.push_back(pElement); } void Del(STR_TYPE*& pElement) { if(pElement) { delete pElement; pElement = 0; } } void Print(const STR_TYPE* pElement) { cout << pElement->name << "\t" << pElement->num << "\t" << pElement->price << endl; } double Add(double r, const STR_TYPE* pA) { return r + pA->price; } int main(void) { Insert("a", 1, 1.2); Insert("b", 2, 2.4); Insert("c", 3, 3.6); for_each(L.begin(), L.end(), Print); double r = 0.0; r = accumulate(L.begin(), L.end(), r, Add); cout << "Result: " << r << endl; for_each(L.begin(), L.end(), Del); L.clear(); for_each(L.begin(), L.end(), Print); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值