将数组复制到向量中

发布于 2024-11-15 08:31:00 字数 1754 浏览 2 评论 0原文

我编写了一个小程序:

void showrecord()
{
     char *a[]={ "O_BILLABLE_ACCOUNT","O_CUSTOMER_TYPE_INDICATOR",
                 "O_A_PARTY_MSISDN_ID","O_A_PARTY_EQUIPMENT_NUMBER",
                 "O_A_PARTY_IMSI","O_A_PARTY_LOCATION_INFO_CELL_ID",
                 ...  
               };

     vector<std::string> fields(a,a+75);

     cout<<"done!!!"<<endl;
}

int main()
{
     showrecord();
}

我有字符串文字数组,我希望将它们复制到向量中。 我没有找到任何其他简单的方法来做到这一点:(。或者如果有任何直接的方法来初始化向量而不使用数组,那将非常有帮助。 这是我在 UNIX 上运行可执行文件后转储核心。 它给了我一个警告,例如:

Warning 829: "test.cpp", line 12 
# Implicit conversion of string literal to 'char *' is deprecated.
D_TYPE","O_VARCHAR_5","O_VARCHAR_6","O_VARCHAR_7","O_VARCHAR_8","O_VARCHAR_9"};

但是相同的代码在 Windows 上运行良好,没有任何问题。 我在 HPUX 上使用编译器 aCC。

请帮忙! 编辑 下面是转储的堆栈跟踪。

(gdb) where
#0  0x6800ad94 in strlen+0xc () from /usr/lib/libc.2
#1  0xabc0 in std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string<char,std::char_traits<char>,std::allocator<char>>+0x20 ()
#2  0xae9c in std<char const **,std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>::uninitialized_copy+0x60 ()
#3  0x9ccc in _C_init_aux__Q2_3std6vectorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc__TQ2_3std9allocatorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc____XTPPCc_FPPCcT118_RW_is_not_integer+0x2d8
    ()
#4  0x9624 in showrecord () at test.cpp:13
#5  0xdbd8 in main () at test.cpp:21

I have written a small program:

void showrecord()
{
     char *a[]={ "O_BILLABLE_ACCOUNT","O_CUSTOMER_TYPE_INDICATOR",
                 "O_A_PARTY_MSISDN_ID","O_A_PARTY_EQUIPMENT_NUMBER",
                 "O_A_PARTY_IMSI","O_A_PARTY_LOCATION_INFO_CELL_ID",
                 ...  
               };

     vector<std::string> fields(a,a+75);

     cout<<"done!!!"<<endl;
}

int main()
{
     showrecord();
}

I have array of string literals and i want them to be copied into a vector.
I did not find any other easy way to do it :(.Or if there is any direct way to initialize the vector without using the array ,that would be very much helpful.
This is dumping the core after i run the executable on unix.
It gives me a warning though like :

Warning 829: "test.cpp", line 12 
# Implicit conversion of string literal to 'char *' is deprecated.
D_TYPE","O_VARCHAR_5","O_VARCHAR_6","O_VARCHAR_7","O_VARCHAR_8","O_VARCHAR_9"};

But the same code is running fine on windows without any problem.
I am using the compiler aCC on HPUX.

Please help!
EDIT
below is teh stacktrace of the dump.

(gdb) where
#0  0x6800ad94 in strlen+0xc () from /usr/lib/libc.2
#1  0xabc0 in std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string<char,std::char_traits<char>,std::allocator<char>>+0x20 ()
#2  0xae9c in std<char const **,std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>::uninitialized_copy+0x60 ()
#3  0x9ccc in _C_init_aux__Q2_3std6vectorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc__TQ2_3std9allocatorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc____XTPPCc_FPPCcT118_RW_is_not_integer+0x2d8
    ()
#4  0x9624 in showrecord () at test.cpp:13
#5  0xdbd8 in main () at test.cpp:21

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

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

发布评论

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

评论(3

白首有我共你 2024-11-22 08:31:00

为什么是75?

更改

vector<std::string> fields(a,a+75);

vector<std::string> fields(a, a + sizeof a / sizeof *a);

没有可以说是“更好”的方法来初始化 C++03 的向量,但对于 C++0x,您可以使用更方便的语法,无需使用 C 数组:

std::vector<std::string> fields {
    "O_BILLABLE_ACCOUNT",
    // ...
};

Why 75?

Change

vector<std::string> fields(a,a+75);

to

vector<std::string> fields(a, a + sizeof a / sizeof *a);

There's no arguably 'better' way to initialize your vector for C++03, but for C++0x you have access to a more convenient syntax, dispensing with the C array:

std::vector<std::string> fields {
    "O_BILLABLE_ACCOUNT",
    // ...
};
尾戒 2024-11-22 08:31:00

尝试使用 const char* a[] 而不是 char* a[]。字符串文字的类型是 const char*,而不是 char*,因此您会收到警告。

Try const char* a[] instead of char* a[]. String literals are of type const char*, not char*, and hence you get the warning.

自我难过 2024-11-22 08:31:00

这是可能的解决方案,IMO 更通用一些 - 使用可重用的函数,可处理任何大小的字符串数组:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

template <typename Array, std::size_t Size>
std::vector<std::string> make_vector(Array (&ar)[Size])
{
    std::vector<std::string> v(ar, ar + Size);
    return v;
}

int main()
{
     char const* a[] = { "Aa","Bb", "Cc","Dd", "Ee","Ff" };

     // copy C-array to vector
     std::vector<std::string> fields = make_vector(a);

     // test
     std::copy(fields.begin(), fields.end(),
               std::ostream_iterator<std::string>(std::cout, "\n"));
}

Here is possible solution which IMO is a little bit more general - uses reusable function that works with string arrays of any size:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

template <typename Array, std::size_t Size>
std::vector<std::string> make_vector(Array (&ar)[Size])
{
    std::vector<std::string> v(ar, ar + Size);
    return v;
}

int main()
{
     char const* a[] = { "Aa","Bb", "Cc","Dd", "Ee","Ff" };

     // copy C-array to vector
     std::vector<std::string> fields = make_vector(a);

     // test
     std::copy(fields.begin(), fields.end(),
               std::ostream_iterator<std::string>(std::cout, "\n"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文