如何在 c++ 中添加多个字符串

发布于 2024-08-22 15:34:25 字数 145 浏览 5 评论 0原文

据我所知,C++ 只允许将 2 个字符串添加在一起,即: s = s1 + s2

但是如何将多个字符串相加呢?喜欢:

s = s1 + s2 + s3 + s4 + ... + sn

As I know that C++ only allows to add 2 strings together, i.e:
s = s1 + s2

But how can I add many strings together? Like:

s = s1 + s2 + s3 + s4 + ... + sn

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

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

发布评论

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

评论(6

与之呼应 2024-08-29 15:34:25

如果您尝试附加 std::string 类的字符串对象,这应该可行。

string s1 = "string1";
string s2 = "string2";
string s3 = "string3";

string s = s1 + s2 + s3;

string s = string("s1") + string("s2") + string("s3") ...

If you're trying to append string objects of std::string class, this should work.

string s1 = "string1";
string s2 = "string2";
string s3 = "string3";

string s = s1 + s2 + s3;

OR

string s = string("s1") + string("s2") + string("s3") ...
梦幻的味道 2024-08-29 15:34:25

首先,你可以很好地执行 +sn 操作。不过,假设您在 C++03 上使用 std::basic_string 字符串,则需要指数二次方(请参阅注释)时间。

您可以将 std::basic_string::appendstd::basic_string::reserve 配合使用,以 O(n) 的时间连接字符串时间。

编辑:例如

string a;
//either
a.append(s1).append(s2).append(s3);
//or
a.append("I'm a string!").append("I am another string!");

First of all, you can do the +sn thing just fine. Though it's going to take exponential quadradic(see comments) time assuming you're using std::basic_string<t> strings on C++03.

You can use the std::basic_string<t>::append in concert with std::basic_string<t>::reserve to concatenate your string in O(n) time.

EDIT: For example

string a;
//either
a.append(s1).append(s2).append(s3);
//or
a.append("I'm a string!").append("I am another string!");
欲拥i 2024-08-29 15:34:25
s = s1 + s2 + s3 + .. + sn;

尽管它可能会创建很多临时对象(一个好的优化编译器应该会有所帮助),但它会起作用,因为它会有效地被解释为:

string tmp1 = s1 + s2;
string tmp2 = tmp1 + s3;
string tmp3 = tmp2 + s4;
...
s = tmpn + sn;

保证不创建临时对象的另一种方法是:

s = s1;
s += s2;
s += s3;
...
s += sn;
s = s1 + s2 + s3 + .. + sn;

will work although it could create a lot of temporaries (a good optimizing compiler should help) because it will effectively be interpreted as:

string tmp1 = s1 + s2;
string tmp2 = tmp1 + s3;
string tmp3 = tmp2 + s4;
...
s = tmpn + sn;

An alternate way that is guaranteed not to create temporaries is:

s = s1;
s += s2;
s += s3;
...
s += sn;
以往的大感动 2024-08-29 15:34:25

std::ostringstream 是为此构建的,请参阅示例此处。这很简单:

std::ostringstream out;
out << "a" << "b" << "c" << .... << "z";
std::string str( out.str());

std::ostringstream is build for that, see example here. It's easy:

std::ostringstream out;
out << "a" << "b" << "c" << .... << "z";
std::string str( out.str());
冷默言语 2024-08-29 15:34:25

使用模板添加字符串、char* 和 char's 来形成字符串

strlen:-

#include <iostream>
#include <cstring>

// it_pair to wrap a pair of iterators for a for(:) loop
template<typename IT> 
class it_pair
    {
    IT b;
    IT e;
public:
    auto begin() const
        {
        return b;
        }
    auto end() const
        {
        return e;
        }
    };

// string length
template<typename S> auto strlen(const S& s) -> decltype(s.size())
    {
    return s.size();
    }

auto strlen(char c) -> size_t
    {
    return 1u;
    }

auto strlen(const std::initializer_list<char>& il) -> size_t
    {
    return il.size();
    }

template<typename IT>
auto strlen(const it_pair<IT>& p)
    {
    auto len = size_t{};
    for(const auto& s:p)
        len += strlen(s);
    return len;
    }

template<typename S, typename ...SS> auto strlen(S s, SS&... ss) -> size_t
    {
    return strlen(s) + strlen(ss...);
    }

附加字符串

// terminate recursion
template<typename TA, typename TB>
void append(TA& a, TB& b)
    {
    a.append(b);
    }

// special case for a character
template<>
void append<std::string, const char>(std::string& a, const char& b)
    {
    a.append(1, b);
    }

// special case for a collection of strings
template<typename TA, typename TB>
void append(TA& a, const it_pair<TB>& p)
    {
    for(const auto& x: p)
        a.append(x);
    }

// recursion append
template<typename TA, typename TB, typename ...TT>
void append(TA& a, TB& b, TT&... tt)
    {
    append(a, b);
    append(a, tt...);
    }

template<typename ...TT>
std::string string_add(const TT& ... tt)
    {
    std::string s;
    s.reserve(strlen(tt...));
    append(s, tt...);
    return s;
    }

template<typename IT>
auto make_it_pair(IT b, IT e)
    {
    return it_pair<IT>{b, e};
    }

template<typename T>
auto make_it_pair(const T& t)
    {
    using namespace std;
    return make_it_pair(cbegin(t), cend(t));
    }

main 示例

int main()
    {
    const char * s[] = {"vw", "xyz"};
    std::vector<std::string> v{"l", "mn", "opqr"};
    std::string a("a");
    std::string b("bc");
    std::string c("def");
    std::cout << string_add(a, b+c, "ghij", make_it_pair(v), 'k', make_it_pair(s));
    }

Use a template to add strings, char* and char's to form a string

strlen:-

#include <iostream>
#include <cstring>

// it_pair to wrap a pair of iterators for a for(:) loop
template<typename IT> 
class it_pair
    {
    IT b;
    IT e;
public:
    auto begin() const
        {
        return b;
        }
    auto end() const
        {
        return e;
        }
    };

// string length
template<typename S> auto strlen(const S& s) -> decltype(s.size())
    {
    return s.size();
    }

auto strlen(char c) -> size_t
    {
    return 1u;
    }

auto strlen(const std::initializer_list<char>& il) -> size_t
    {
    return il.size();
    }

template<typename IT>
auto strlen(const it_pair<IT>& p)
    {
    auto len = size_t{};
    for(const auto& s:p)
        len += strlen(s);
    return len;
    }

template<typename S, typename ...SS> auto strlen(S s, SS&... ss) -> size_t
    {
    return strlen(s) + strlen(ss...);
    }

appending strings

// terminate recursion
template<typename TA, typename TB>
void append(TA& a, TB& b)
    {
    a.append(b);
    }

// special case for a character
template<>
void append<std::string, const char>(std::string& a, const char& b)
    {
    a.append(1, b);
    }

// special case for a collection of strings
template<typename TA, typename TB>
void append(TA& a, const it_pair<TB>& p)
    {
    for(const auto& x: p)
        a.append(x);
    }

// recursion append
template<typename TA, typename TB, typename ...TT>
void append(TA& a, TB& b, TT&... tt)
    {
    append(a, b);
    append(a, tt...);
    }

template<typename ...TT>
std::string string_add(const TT& ... tt)
    {
    std::string s;
    s.reserve(strlen(tt...));
    append(s, tt...);
    return s;
    }

template<typename IT>
auto make_it_pair(IT b, IT e)
    {
    return it_pair<IT>{b, e};
    }

template<typename T>
auto make_it_pair(const T& t)
    {
    using namespace std;
    return make_it_pair(cbegin(t), cend(t));
    }

main example

int main()
    {
    const char * s[] = {"vw", "xyz"};
    std::vector<std::string> v{"l", "mn", "opqr"};
    std::string a("a");
    std::string b("bc");
    std::string c("def");
    std::cout << string_add(a, b+c, "ghij", make_it_pair(v), 'k', make_it_pair(s));
    }
始终不够 2024-08-29 15:34:25

如果您希望能够

  • 高效地执行此操作,即不会产生二次时间
  • 而不覆盖原始变量
  • 而不创建临时变量

那么这可以完成工作

auto s = std::string(s1).append(s2).append(s3).append(sn);  

如果你喜欢格式良好的东西

auto s = std::string(s1).append(s2)
                        .append(s3)
                        .append(sn)

If you want to be able to do this

  • Efficiently, i.e without incurring quadratic time
  • Without overwriting the original variable
  • Without creating temporary variables

Then this will do the job

auto s = std::string(s1).append(s2).append(s3).append(sn);  

And if you like things nicely formatted

auto s = std::string(s1).append(s2)
                        .append(s3)
                        .append(sn)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文