开发者

How to store fixed length strings inside a std::vector

开发者 https://www.devze.com 2023-03-29 08:48 出处:网络
I want to mimic a structure: char [][40] = { \"Stack\", \"Overflow\", \"Exchange\", \"Network\" }; using a std::vector, so I can populate it at runtime and dynamically change the size of the vector

I want to mimic a structure:

char [][40] = { "Stack", "Overflow", "Exchange", "Network" };

using a std::vector, so I can populate it at runtime and dynamically change the size of the vector, but keeping the member elements located inside fixed size blocks.

Static initialization is not my question - I can do that using boost::assign or o开发者_StackOverflow社区ther tricks.


I'd use something like Boost.Array:

typedef boost::array<char, 40> arr_t;
std::vector<arr_t> vec;
{
    arr_t arr = { "Stack" };
    vec.push_back(arr);
}
{
    arr_t arr = { "Overflow" };
    vec.push_back(arr);
}
{
    arr_t arr = { "Exchange" };
    vec.push_back(arr);
}
{
    arr_t arr = { "Network" };
    vec.push_back(arr);
}

If you're using a reasonably recent compiler, instead of Boost you can probably use std::array<> (C++11; #include <array>) or std::tr1::array<> (C++03 with TR1; #include <array> or #include <tr1/array>, depending on platform).


struct fixed_string { 
    char data[40];

    fixed_string(char const *init);
};

std::vector<fixed_string> whatever;

If you have C++11 (or at least TR1), you probably want to use std::array instead of fixed_string. I think Boost has an equivalent as well.

In case anybody's wondering why I put it in a struct, instead of creating a vector of array directly: because items in a vector need to be copyable and assignable, and a bare array is neither.


May be vector in vector helps. Here size is fixed, strings could be used in pure C.

vector< vector<char> > vector_of_strings;

// Add new string
vector_of_strings.push_back( vector<char>(40) );
// use the string
strcpy( &vector_of_strings[0][0], "text" );


Parapura's answer is correct, but you will be storing the pointers to the strings. If the original character array falls out of scope you will lose the information posted. If this vector is used elsewhere, it should allocate (and deallocate!) it's own memory. This can be done when the input is taken.

Here's a sample program that does that.

#include <vector>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::vector;

int main()
{
    int numEntries = 4;
    const int strlen = 40;

    // Parapura's correct answer, but let's expand this further
    vector<char*> strvec;
    char* input = 0;
    int i;

    cout << "Enter four names." << endl;
    for (i=0; i<numEntries; i++)
    {
        // Allocate some memory to store in the vector
        input = new char[strlen];
        cout << "Name: ";
        cin.get(input, strlen);
        cin.ignore(strlen, '\n');

        // Push the populated memory into the vector.
        // Now we can let 'input' fall out of scope.
        strvec.push_back(input);
    }
    cout << endl;

    /* -- cool code here! -- */

    cout << "List of names." << endl;
    for (i=0; i<numEntries; i++)
    {
        cout << strvec[i] << endl;
    }

    /* -- more cool code! -- */

    // don't forget to clean up!
    for (i=0; i<numEntries; i++)
    {
        delete[] strvec[i];
    }

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消