【问题标题】:Create span of string_views from C string array从 C 字符串数组创建 string_views 的范围
【发布时间】:2021-01-10 04:17:21
【问题描述】:

给定某个函数 void func(std::span<std::string_view>),如何以最有效的方式为该函数提供 C 字符串的原始数组 const char**

据我了解,这应该是可能的,因为 std::string_view 可以从 C 字符串构造,std::span 可以从指针 + 大小构造。但是,我似乎无法弄清楚正确的语法。

下面是一些通过迭代字符串数组中的每个字符串来创建std::vector<std::string_view> 的最小代码:

#include <iostream>
#include <string>
#include <span>
#include <vector>

static void func(std::span<std::string_view> strings)
{
    // Just print the strings...
    std::cout << "strings:\n";
    for (const auto& str : strings)
        std::cout << str << "\n";
    std::cout << std::endl;
}

int main()
{
    // Raw C string array
    const char* raw_strings[3] = {
        "This is a string",
        "This is also a string",
        "And then another string"
    };
    
#if 1
    // This works
    std::vector<std::string_view> s;
    for (std::size_t i = 0; i < 3; i++)
        s.push_back( std::string_view{ raw_strings[i] } ); 
#else
    // This does not work
    std::span<std::string_view> s{ raw_strings, (std::size_t)3 };
#endif
        
    func(s);
}

这是完成繁重工作的 coliru 链接:https://coliru.stacked-crooked.com/a/cb8fb8ebbc962d45

【问题讨论】:

标签: c++ c++20 string-view std-span


【解决方案1】:

如果您考虑一下,要执行您要求的操作,编译器需要找到某个地方来存储 3 个 std::string_views,并且没有这样的地方,因为 std::span 实际上并没有为其引用的元素分配任何存储空间.

所以,AFAICS,没有直接的方法可以做到这一点。


编辑:我认为这是最便宜的方法,因为它不涉及免费商店的分配。但请注意,在使用跨度时数组不会超出范围:

std::array <std::string_view, sizeof (raw_strings) / sizeof (raw_strings [0])> sa;
std::copy (std::begin (raw_strings), std::end (raw_strings), std::begin (sa));
std::span <std::string_view> s { sa };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2021-11-09
    • 2023-04-03
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多