4

I need a structure as follow:

Figure1

The structure must hold fixed size std::strings so that the number of its elements is finit (100 - 10000000).

I would like to be able to access each element randomly as follow:

std::string Temp = MyStrcuture[i];

or

MyStrcuture[i] = std::string Temp;

I have to use the fastest structure with no (possibly) memory leak.

Which one is better for me?

  1. std::string* MyStrcuture = new std::string[Nu_of_Elements];
  2. std::queue< std:string> MyStrcuture(Nu_of_Elements);
  3. std::vector< std:string> MyStrcuture(Nu_of_Elements);
  4. boost::circular_buffer< std::string> MyStrcuture(Nu_of_Elements);
  5. Your suggestion?
Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40

3 Answers3

12
std::vector< std:string> MyStrcuture(Nu_of_Elements);

Vector is the best fit for your requirements. It supports index-based element access as the elements are stored in continuous memory addresses, and has flexibility with size.


  1. std:string* MyStrcuture = new std::string[Nu_of_Elements]; No

    C++ STL vector vs array in the real world

  2. std::queue< std:string> MyStrcuture(Nu_of_Elements); No

    How do I get the nth item in a queue in java?
    Index-based element access is not supported.

  3. std::vector< std:string> MyStrcuture(Nu_of_Elements); Yes

    Clean-up : The vector's destructor automatically invokes the destructor of each element in the vector.

  4. Boost::circular_buffer< std::string> MyStrcuture(Nu_of_Elements); No

    Same reason as second one. Know more

Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • 2
    Instead of `std::vector`, `std::vector>` makes more sense from a memory perspective. – themagicalyang Nov 02 '16 at 06:37
  • 1
    @themagicalyang: yes and no, since it would require a huge block of contiguous memory, which may not be available in e.g. a fragmented 32 bit address space. – Matteo Italia Nov 02 '16 at 06:49
  • @MatteoItalia I concur. But if that is the case, then some error checking for allocation is due. But contiguous memory memory would still be my first choice. If not fall back, on a different allocation pattern. – themagicalyang Nov 02 '16 at 06:54
4

Well, since your string have fixed size, if you don't have dedicated requirement when processing string and have enough free memory for contiguous allocation. You can use std::array< char, 400 > or std::unique_ptr< char* > instead of std::string.

  1. You have to manage memory in C way. consider smart pointer
  2. std::queue doesn't have random access, Access c++ queue elements like an array

  3. std::vector is suitable if the number of string will be changed. However, the clear() function just call the destructor of elements, not free vector allocated memory (you can check the capacity after clear).

  4. After reading boost documentation. The random access circular buffer is suitable if your number of string have an upper limit (that you said 10 millions). But its a waste of memory if actually you have so few strings. So I suggest to use with smart pointer.

  5. If your number of string are fixed and unchanged from the beginning. You can have a look at C++11 array container

Community
  • 1
  • 1
khôi nguyễn
  • 626
  • 6
  • 15
0

If number of elements and length is fixed and memory is critical, you may consider using plain char array, which provides minimal memory overhead and fast accessibility. Your code will look like this:

char* MyStructure = new char[n * 401];
memset(MyStructure, 0, n * 401);

std::string Temp = MyStructure[i * 401]; // Get value
strcpy(MyStructure[i * 401], Temp.c_str()); // Put value

401 here is for 400 bytes of your string and 1 trailing zero.

Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34
  • 2
    This is the proper solution. Why would one go for dynamically allocated std::string when the strings are fixed sized and can be held in character array. More benefits to this is fast access, and no memory hops. Though I would use `std::array` for the string instead. So this could turn out to be `std::vector` – themagicalyang Nov 02 '16 at 06:33
  • 1
    I'm sure you mean a *trailing* zero. – celtschk Nov 02 '16 at 06:39
  • "1 leading zero"? Did you mean zero-termination? – khôi nguyễn Nov 02 '16 at 06:41
  • You are not allocating the actual memory for the characters - there you allocated just an array of pointers. That `strcpy` is going to segfault badly. – Matteo Italia Nov 02 '16 at 06:47