如何在 std::vector 中存储固定长度的字符串

发布于 2024-11-30 12:11:04 字数 258 浏览 0 评论 0原文

我想模仿一个结构:

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

使用 std::vector,这样我就可以在运行时填充它并动态更改 vector 的大小,但保持成员元素的位置在固定大小的块内。

静态初始化不是我的问题 - 我可以使用 boost::assign 或其他技巧来做到这一点。

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 other tricks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赤濁 2024-12-07 12:11:04

我会使用类似 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);
}

如果你使用的是合理的最近的编译器,您可以使用 std::array<> (C++11; #include) 来代替 Boost std::tr1::array<>(带有 TR1 的 C++03;#include#include,取决于平台)。

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).

时光瘦了 2024-12-07 12:11:04
struct fixed_string { 
    char data[40];

    fixed_string(char const *init);
};

std::vector<fixed_string> whatever;

如果您使用 C++11(或至少 TR1),您可能需要使用 std::array 而不是 fixed_string。我认为 Boost 也有类似的东西。

如果有人想知道为什么我将它放在 struct 中,而不是直接创建数组向量:因为 vector 中的项目需要可复制和可分配,并且是裸露的数组都不是。

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.

荒芜了季节 2024-12-07 12:11:04

可能是 vector 中的 vector 有帮助。这里大小是固定的,字符串可以在纯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" );

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" );
谁的新欢旧爱 2024-12-07 12:11:04

Parapura 的答案是正确的,但您将存储指向字符串的指针。如果原始字符数组超出范围,您将丢失发布的信息。如果这个向量在其他地方使用,它应该分配(和释放!)它自己的内存。这可以在获取输入时完成。

这是一个执行此操作的示例程序。

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文