stl

How-to return a const std::vector<Object *const>?

无人久伴 提交于 2020-01-11 11:51:10
问题 I have a class with a container (containing pointer) as a member: MyClass{ private: std::vector<MyObject*> _VecMyObjs; public: const std::vector<MyObject* const> GetVecMyObj(); } Now I try to implement GetVecMyObj(). Here is what I came up with... const vector<MyObject *const> ACI_CALL MyClass::GetVecMyObjs() { const vector<MyObject *const> VecMyObjs; VecMyObjs.assign( _VecMyObjs.begin(), _VecMyObjs.end()); return VecMyObjs; } But of course the compiler is warning me, that I use the assign

Is there any boost/stl container which supports the following operation?

亡梦爱人 提交于 2020-01-11 10:22:03
问题 I was looking for stl/boost container which can provide following functionality: Auto insert element in sorted order. (log n) Return the index/depth of element from starting node. (log n) If there isn't one, what would be the best way to achieve this? I am thinking of a solution using a doubly link list. Would that be good choice to solve this problem ? 回答1: UPDATE You need an order statistic tree. The C++ Standard Library doesn't have any, nor does it offer an easy way to implement one, see

Is there any boost/stl container which supports the following operation?

戏子无情 提交于 2020-01-11 10:21:52
问题 I was looking for stl/boost container which can provide following functionality: Auto insert element in sorted order. (log n) Return the index/depth of element from starting node. (log n) If there isn't one, what would be the best way to achieve this? I am thinking of a solution using a doubly link list. Would that be good choice to solve this problem ? 回答1: UPDATE You need an order statistic tree. The C++ Standard Library doesn't have any, nor does it offer an easy way to implement one, see

STL 常用方法

我是研究僧i 提交于 2020-01-11 08:43:27
1. 选择 C++ 刷算法的理由 1、C++ 速度快(C 不是更快吗,Java 太慢了) 2、C++ 有 STL(什么是 STL)——性能强大,使用方便的标准库 3、如何使用 STL 进行高效刷算法 4、好处:刷算法,学习成本低 5、如何从 C 到 C++(仅基础语法到刷算法程度) 俗话说:磨刀不误砍柴工,不会 C++ 仍然可以刷算法,但是效率相对很低。在 ACM 或各类程序算法竞赛中相比于 Java 代码的冗长,C 的繁琐,Python 的性能低下,C++ 以兼顾简洁和高效脱颖而出。 2.输入输出 C++ 保留了 C 的 scanf 和 printf,额外增加了 cin 和 cout。 Example: 2.1 C 程序中的输入输出 int a; scanf("%d", &a); printf("%d", a); 2.2 C++ 程序中的输入输出 int a; cin >> a; cout << a; 2.3 C++ 程序中的连续输入输出 int a, b, c; cin >> a >> b >> c; cout << a << b << c; 2.4 C++ 优雅地换行 cout << 1; cout << endl; cout << 2; cout << 3 << endl << endl; Notice: cin、cout 使用虽然很方便,但是比 scanf、printf

STL Containers allocation placement new

柔情痞子 提交于 2020-01-11 05:38:09
问题 I couldn't find an exact answer to this question and hence posting here. When I think of vector, it needs to build objects in a contiguous memory location. This means that vector keeps memory allocated and have to do an in-place construction (=placement new) of objects being pushed into it. Is this a valid assumption? Also, does this mean the container is manually invoking the destructor rather than calling delete? Are there any other assumptions that I am missing here? Does this mean I can

STL Map with custom compare function object

给你一囗甜甜゛ 提交于 2020-01-11 04:38:11
问题 I want to use the STL's Map container to lookup a pointer by using binary data as a key so I wrote this custom function object: struct my_cmp { bool operator() (unsigned char * const &a, unsigned char * const &b) { return (memcmp(a,b,4)<0) ? true : false; } }; And using it like this: map<unsigned char *, void *, my_cmp> mymap; This compiles and seems to work, but I'm not sure what an "unsigned char * const &" type is and why it didn't work with just "unsigned char *"? 回答1: You need to provide

How to make std::vector from other vector with specific filter?

点点圈 提交于 2020-01-11 04:23:04
问题 I have a std::vector filled with objects. I want to filter and copy all the elements for which some predicate returns true in to a new std::vector . I've looked at find and search functions but they only return iterators. I'm using ObjC++ so I can use block functions as well as functors, if it helps. Can't use C++11 functions though. 回答1: If you have C++11 then use std::copy_if as suggested in Eugen's answer. Otherwise, you can use std::remove_copy_if with appropriate modifications to your

C++ map performance - Linux (30 sec) vs Windows (30 mins) !

倾然丶 夕夏残阳落幕 提交于 2020-01-10 19:46:33
问题 I need to process a list of files. The processing action should not be repeated for the same file. The code I am using for this is - using namespace std; vector<File*> gInputFileList; //Can contain duplicates, File has member sFilename map<string, File*> gProcessedFileList; //Using map to avoid linear search costs void processFile(File* pFile) { File* pProcessedFile = gProcessedFileList[pFile->sFilename]; if(pProcessedFile != NULL) return; //Already processed foo(pFile); //foo() is the action

C++ map performance - Linux (30 sec) vs Windows (30 mins) !

為{幸葍}努か 提交于 2020-01-10 19:46:08
问题 I need to process a list of files. The processing action should not be repeated for the same file. The code I am using for this is - using namespace std; vector<File*> gInputFileList; //Can contain duplicates, File has member sFilename map<string, File*> gProcessedFileList; //Using map to avoid linear search costs void processFile(File* pFile) { File* pProcessedFile = gProcessedFileList[pFile->sFilename]; if(pProcessedFile != NULL) return; //Already processed foo(pFile); //foo() is the action

STL笔记

非 Y 不嫁゛ 提交于 2020-01-10 19:05:50
STL的基本概念:   1-容器:是可容纳各种类型的数据结构,是 类模板 。   2-迭代器:是用于依次存放容器中的元素,类似 指针 。   3-算法: 是用于操作容器中元素的 函数模板 。        sort() 用来对 vector 中的数据进行排序。        find() 用来搜索 list 中的对象。       算法本身与他们操作的数据类型无关,因此他们可以在从简单数组到高度复杂容器的任何数据结构上使用。 如 int a[100]:   该数组就是容器,而int * 类型的指针变量就可以作为迭代器,sort算法可以作用在该容器上,对其排序:   sort(a,a+50); //对前50个元素进行排序 容器种类:   顺序容器: vector(数组) ,deque (双向队列),list(双向链表)   关联容器:set ,multiset ,map ,multimap // 都能对输入数据进行排序   容器适配器: stack ,queue ,priority_queue   PS: 对象被插入容器中时,被插入的是对象的一个 复制品 。 容器 容器上的迭代器访问 vector 随机访问(通过下标就能取到元素) deque 随机访问 list 双向(不能用下标取元素) set/multiset 双向 map/multimap 双向 stack 不支持迭代器