将多个字符串组合为c字符串

问题描述

让我开始说,我知道这并不理想。我在C ++中使用C字符串在做什么? 很好的问题,但是我使用的软件的某些旧服务不存储动态内存。 话虽如此。我正在尝试解决一个常见的问题

  • 将字符串文字转换为char *
  • c种串联字符串的方法可能导致缓冲区溢出。

我遇到了c字符串问题。我发现这个线程有很多很棒的方法获取字符串文字,整数,浮点数之类的东西,并将其串联成一个字符串。对于这些示例中的几乎任何一个,我都可以执行以下操作:

std::string s = stringger("word1",' ',12345,"word2");
const char *c = s.c_str();

下面是我希望做的一个例子。再次编译,但不返回任何内容

#include <iostream>
#include <string>
#include <sstream>

template< typename ... Args >
const char* stringer(Args const& ... args )
{
    std::ostringstream stream;
    using List= int[];
    (void)List{0,( (void)(stream << args),0 ) ... };

    return stream.str().c_str();
}

int main()
{
    const char *s = stringer("hello",23,3.14," Bye! " );

    std::cout << s << '\n';

但是,当我尝试更改返回类型时,我什么也没得到。它可以编译,但我假设它返回null。我想念什么?

Concatenation of strings thread.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)