Std :: vector, std :: problems with maps and memory

I am writing code that inserts rows from a database into a vector. Then the vectors are saved on std :: map. This architecture allows me to logically split data sets (vectors) based on a map key.

In my code, I will retrieve a data set (i.e. a vector) from std :: map, add / remove lines or execute some other logic on it, and then insert the vector back into the map (all this continues during the () loop) .

I have two questions, both of which are related to the (potentially) large number of elements stored in the vector. A vector can contain from several tens to several tens of thousands of elements. It is not possible to know in advance how many records will be retrieved from the database. Therefore, the std :: vector memory management scheme (i.e., alloc / dealloc) is of great importance for efficient memory use and for preventing unnecessary (memory) fragmentation:

My questions:

  • Given the potentially large number of elements a string can store, ideally I would like to allocate / dealloc from a memory pool. But I don’t know how to use std :: vector with a memory pool, and I don’t know if this will be unnecessarily complicated. If this is excessive (or too complicated), then my other idea is to pre-allocate a fixed memory slab when the vector is first created. But it will also probably be too simplistic, as the number of elements will probably vary widely from one vector instance to another, which will lead to fragmentation (memory), etc., Not to mention the inefficient use of memory.

    What is the recommended approach here?

  • , std:: map ( STL IIRC) , , , . SmartPointerMap arround stl:: map, .

    ?. , . , , ( SmartPointerMap)?

[]

Linux (Ubuntu 9.10) gcc 4.4.1

+3
4

, vector data_type, key_type, , . std::map::operator[]() -const data_type, , std::map::find(), .

, ? std::swap() .

, vector capacity() . , vector capacity(), , push_back() . , .

vector key_type, , . , , , , std::map .

, , , . , . , , , ( - , ). , , ; , .

+6

Wrt # 1, GCC/Linux (ptmalloc) (aka ) (< = 64 , ). , Boost.Pool , Allocator. bk1e, , , .

, /, vector<T>::reserve(), .

, .

+2

2:

using namespace std;

map<string, vector<int>* > foo;
vector<int>* pointer= foo["thekey"];

():

#include<tr1/shared_ptr.h>
using namespace std::tr1;
using namespace std;

map<string, shared_ptr<vector<int> > > foo;
shared_ptr<vector<int> > pointer= foo["thekey"];

№1, , :

map<string, vector<int, myallocator<int> >* > foo;

, , , . ( , .)

+1

C/++.

db . , , , MMU , .

() /, ISAM, Relational Religion, , . # 1

, /, 1 /, ( / ) , . , db. struct.member .

, / , struct.member .

/, , db /.

, , , /, malloc() realloc() , "" . ISAM, , realloc(). . ISAM , , .

0

Source: https://habr.com/ru/post/1733758/


All Articles