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?
8
  • 1
    Is queue even randomly accessible? Commented Nov 2, 2016 at 6:06
  • Also, does C++ have a hash table? It might be nice. Commented Nov 2, 2016 at 6:07
  • unordered_map comes close to hashtable in C++ Commented Nov 2, 2016 at 6:10
  • Fixed-size std::strings? That's an unusual std::string. Commented Nov 2, 2016 at 6:17
  • there's no Std:string or Std:vector in C++ Commented Nov 2, 2016 at 6:22

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Instead of std::vector<std::string>, std::vector<std::array<char,401>> makes more sense from a memory perspective.
@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.
@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.
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

Comments

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.

4 Comments

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<char,401> for the string instead. So this could turn out to be std::vector<std::array<char,401>
I'm sure you mean a trailing zero.
"1 leading zero"? Did you mean zero-termination?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.