简体   繁体   English

如何使用 std:string 参数迭代可变参数函数?

[英]How to iterate over variadic function with std:string arguments?

void foo(std::string arg, ...) {

   // do something with every argument

}

Lets say I want to be able to take every string argument and append an exclamation mark before printing it out on a new line.假设我希望能够获取每个字符串参数并在将其打印到新行之前附加一个感叹号。

The best way is to use parameters pack .最好的方法是使用参数 pack For example:例如:

#include <iostream>

// Modify single string.
void foo(std::string& arg)
{
    arg.append("!");
}

// Modify multiple strings. Here we use parameters pack by `...T`
template<typename ...T>
void foo(std::string& arg, T&... args)
{
    foo(arg);
    foo(args...);
}

int main()
{
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo(s1, s2);

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

This will print out:这将打印出:

qwe!
asd!

C++17 C++17

Use aparameter pack with a fold expression :使用带有fold expressionparameter pack

#include <iostream>
#include <string>

// Modify multiple strings. Here we use parameters pack by `...T`
template<typename ...T>
void foo(T&... args)
{
    (args.append("!"),...);
}

int main()
{
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo(s1, s2);

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

Here is an iterative solution.这是一个迭代解决方案。 There is a bit of noise in the function call, but no computation of the number of varargs is needed.函数调用中有一点噪音,但不需要计算可变参数的数量。

#include <iostream>
#include <string>
#include <initializer_list>
#include <functional> // reference_wrapper

void foo(std::initializer_list<std::reference_wrapper<std::string>> args) {
    for (auto arg : args) {
        arg.get().append("!");
    }
}

int main() {
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo({s1, s2});

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代可变参数模板类型(不是参数)? - How to iterate over Variadic template types (not arguments)? 没有与可变参数调用std :: forward(const std :: string&)的匹配函数 - No matching function for call std::forward(const std::string &) with variadic arguments 遍历可变参数模板函数并选择指针参数 - iterate over a Variadic Template function and choose pointer arguments 如何在没有 arguments 的 function 中迭代/获取 static 成员? - How do I iterate over/get static members from the variadic template types in a function with no arguments? 如何正确实现具有可变数量的std :: string_view参数的函数? - How to properly implement a function with variadic number of std::string_view arguments? 如何在可变参数函数中的所有参数上调用std :: forward? - How would one call std::forward on all arguments in a variadic function? 如何在没有可变参数 function 的情况下循环函数的 arguments - how to loop over function's arguments without variadic function std :: function到可变参数成员函数,然后绑定可变参数模板参数 - std::function to variadic member function and then bind variadic template arguments 传递std :: array作为模板可变参数的参数 - Passing std::array as arguments of template variadic function 如何将模板可变参数保存到std :: ofstream中? - How to store template variadic arguments into std::ofstream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM