将以 null 结尾的 const char* 字符串数组转换为 std::vector< std::string >

发布于 2024-10-08 03:59:58 字数 478 浏览 0 评论 0 原文

我有一个 Visual Studio 2008 C++ 函数,其中给出了一个以 null 结尾的字符串数组 const char* 以及该数组中字符串数量的计数。

我正在寻找一种巧妙的方法将 const char* 数组转换为 std::vector std::string >

/// @param count - number of strings in the array
/// @param array - array of null-terminated strings
/// @return - a vector of stl strings
std::vector< std::string > Convert( int count, const char* array[] );

Boost 很好,STL 也很好。

I have a Visual Studio 2008 C++ function where I'm given an array of null-terminated strings const char* and a count of the number of strings in that array.

I'm looking for a clever way of turning an array of const char* in to a std::vector< std::string >

/// @param count - number of strings in the array
/// @param array - array of null-terminated strings
/// @return - a vector of stl strings
std::vector< std::string > Convert( int count, const char* array[] );

Boost is fine, STL is fine.

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

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

发布评论

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

评论(4

缘字诀 2024-10-15 03:59:58

像这样的东西?:

vector< string > ret( array, array + count );

Something like this?:

vector< string > ret( array, array + count );
新雨望断虹 2024-10-15 03:59:58

试试这个:

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


int main(int argc,char* argv[])
{
    // Put it into a vector
    std::vector<std::string>    data(argv, argv + argc);

    // Print the vector to std::cout
    std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

Try this:

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


int main(int argc,char* argv[])
{
    // Put it into a vector
    std::vector<std::string>    data(argv, argv + argc);

    // Print the vector to std::cout
    std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
給妳壹絲溫柔 2024-10-15 03:59:58

假设你的函数的签名无意中错误,你的意思是这样的吗?

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

std::vector<std::string> Convert(int count, const char **arr)
{
    std::vector<std::string> vec;
    vec.reserve(count);
    std::copy(arr, arr+count, std::back_inserter(vec));
    return vec;
}

int main()
{
    const char *arr[3] = {"Blah", "Wibble", "Shrug"};
    std::vector<std::string> vec = Convert(3, arr);
    return 0;
}

Assuming that the signature of your function is inadvertently wrong, do you mean something like this?

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

std::vector<std::string> Convert(int count, const char **arr)
{
    std::vector<std::string> vec;
    vec.reserve(count);
    std::copy(arr, arr+count, std::back_inserter(vec));
    return vec;
}

int main()
{
    const char *arr[3] = {"Blah", "Wibble", "Shrug"};
    std::vector<std::string> vec = Convert(3, arr);
    return 0;
}
雨的味道风的声音 2024-10-15 03:59:58

我假设你的数组中的字符串是用 0 分隔的。

std::vector< std::string > Convert( int count, const char* array )
{
   std::vector< std::string > result;

   for(const char* begin = array; count; --count)
   {
    const char* end = begin;
    for(; *end; ++end);
    result.push_back(std::string(begin, end));
    begin = end + 1;
   }

   return result;
}

I assume in your array strings are separated by 0s.

std::vector< std::string > Convert( int count, const char* array )
{
   std::vector< std::string > result;

   for(const char* begin = array; count; --count)
   {
    const char* end = begin;
    for(; *end; ++end);
    result.push_back(std::string(begin, end));
    begin = end + 1;
   }

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