C ++将char *字符串放入std :: vector <string>的更好方法?

时间:2019-01-22 00:08:29

标签: c++ variadic emplace

我正在从任何一个填充字符串向量 字符[] 字符* std :: string 通过将它们包含在std :: vector

此代码有效,但看上去有点笨拙,并使用三个模板来覆盖变量和初始化列表。

这种事情是否还有更规范的习惯用法?

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <stdexcept>


// safe function for putting char* strings into a std::vector<std::string>
template <typename T, typename C>
std::vector<T> & safe_emplace( std::vector<T>& vec, C& c)
{
  if ( !c )  return  vec;

  vec.emplace_back(c);

  return vec;

}


// variadic version
template <typename T, typename C, typename...  Args>
std::vector<T> & safe_emplace( std::vector<T>& vec, C& c, Args... args)
{

  safe_emplace(vec, c); 

  return safe_emplace( vec, args...);

}


// initializer list version
template <typename T>
std::vector<T> & safe_emplace( std::vector<T>& vec, const std::initializer_list<std::string> il)
{
  for (auto& s: il)
    vec.emplace_back(s);

  return vec;
}





int main( int argc, char* argv[])
{
  std::vector<std::string> svec;

  char one[] = "string one";

  char two[] = "string two";

  char three[] = "string three";

  char* d = new char[10];

  char* n = NULL;

  std::strncpy(d, "0123456789\0", 10);

  safe_emplace(svec, one);   // char[]

  safe_emplace(svec, two, three); // variadic

  safe_emplace(svec, d);  // char* 

  safe_emplace(svec, n); // char* NULL

  safe_emplace(svec,   "five", "four", "three", "two", "one", nullptr);

  safe_emplace(svec,   {"A", "B", "C", "D", "E"} );



  for (auto s : svec)
    std::cout << s << "\n";

  delete[] d; // clean up d (new)


  return 0;
}

我特别想处理NULL char *的情况 我决定跳过它,而不是创建一个空字符串。

我尝试过/尝试使用nullptr,但是发现并没有必要 模板。

1 个答案:

答案 0 :(得分:1)

  

此代码有效,但看上去有点笨拙,并使用三个模板来覆盖变量和初始化列表。

     

这种事情是否还有更规范的习惯用法?

并非如此,因为您必须分别处理可变参数模板和initializer_list

  

我特别想处理NULL char *的情况   我决定跳过它,而不是创建一个空字符串。

然后,您应该提供safe_emplace()的重载来与其他类型分开处理char*数据。

请尝试以下类似操作:

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

// rename the actual emplacement functions to avoid unwanted
// recursive loops in the variadic template iteration...

template <typename Container>
Container& do_safe_emplace(Container &c, const char *value)
{
    if (value) c.emplace_back(value);
    return c;
}

template <typename Container>
Container& do_safe_emplace(Container &c, const typename Container::value_type &value)
{
    c.emplace_back(value);
    return c;
}

// this overload is needed to handle when 'args...' becomes blank
// at the end of the variadic template loop iteration...
template <typename Container>
Container& safe_emplace(Container &c)
{
    return c;
}

template <typename Container, typename T, typename... Args>
Container& safe_emplace(Container &c, const T &value, Args... args)
{
    do_safe_emplace(c, value); 
    return safe_emplace(c, args...);
}

template <typename Container, typename T>
Container& safe_emplace(Container &c, const std::initializer_list<T> il)
{
    for (auto& value: il)
        do_safe_emplace(c, value);
    return c;
}

int main()
{
    std::vector<std::string> svec;

    char one[] = "string one";
    char two[] = "string two";
    char three[] = "string three";
    std::string four = "string four";
    char* d = new char[11]; // <- need room for null terminator
    char* n = NULL;

    std::strncpy(d, "0123456789", 11);

    safe_emplace(svec, one);   // char[]
    safe_emplace(svec, two, three, four); // variadic
    safe_emplace(svec, d);  // char* 
    safe_emplace(svec, n); // char* NULL
    safe_emplace(svec, "five", "four", std::string("three"), "two", "one", nullptr);
    safe_emplace(svec, {"A", "B", "C", "D", "E"} );

    for (auto &s : svec)
        std::cout << s << "\n";

    delete[] d; // clean up d (new)

    return 0;
}

Live Demo