【问题标题】:Filling a vector with elements from a list用列表中的元素填充向量
【发布时间】:2013-09-08 13:52:59
【问题描述】:

我正在尝试用指向 C 样式字符串的指针列表中的元素填充字符串向量。

这行得通。

int main()
{
   //typedef char cstr[];
   using cstr = char[];
   std::list<cstr*> lict(10);
   std::vector<std::string> vec;
   for(auto c:lict)
       vec.push_back(*c);
   return 0;
}

但是,当我尝试使用 lict 的范围分配给 vec 时,我收到一条巨大的错误消息。

int main()
{
    using cstr = char[];
    std::list<cstr*> lict(10);
    std::vector<std::string> vec(lict.begin(), lict.end());
    return 0;
}

为什么我不能使用第二种方法?

错误信息是:

/usr/bin/../lib/c++/v1/memory:1681:31: error: no matching constructor for initialization of
      'std::__1::basic_string<char>'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/memory:1608:18: note: in instantiation of function template specialization
      'std::__1::allocator<std::__1::basic_string<char> >::construct<std::__1::basic_string<char>, char (*&)[]>'
      requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/usr/bin/../lib/c++/v1/memory:1492:14: note: in instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<std::__1::basic_string<char> >
      >::__construct<std::__1::basic_string<char>, char (*&)[]>' requested here
            {__construct(__has_construct<allocator_type, pointer, _Args...>(),
             ^
/usr/bin/../lib/c++/v1/vector:933:25: note: in instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<std::__1::basic_string<char> >
      >::construct<std::__1::basic_string<char>, char (*&)[]>' requested here
        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
                        ^
/usr/bin/../lib/c++/v1/vector:1068:9: note: in instantiation of function template specialization
      'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> >
      >::__construct_at_end<std::__1::__list_iterator<char (*)[], void *> >' requested here
        __construct_at_end(__first, __last);
        ^
ex9_14b.cpp:9:29: note: in instantiation of function template specialization
      'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> >
      >::vector<std::__1::__list_iterator<char (*)[], void *> >' requested here
   std::vector<std::string> vec(lict.begin(),lict.end());
                            ^
/usr/bin/../lib/c++/v1/string:1146:31: note: candidate constructor not viable: cannot convert argument of incomplete
      type 'char (*)[]' to 'const_pointer' (aka 'const char *')
    _LIBCPP_INLINE_VISIBILITY basic_string(const_pointer __s);
                              ^
/usr/bin/../lib/c++/v1/string:1136:40: note: candidate constructor not viable: cannot convert argument of incomplete
      type 'char (*)[]' to 'const allocator_type' (aka 'const std::__1::allocator<char>')
    _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a);
                                       ^
/usr/bin/../lib/c++/v1/string:1137:5: note: candidate constructor not viable: cannot convert argument of incomplete type
      'char (*)[]' to 'const std::__1::basic_string<char>'
    basic_string(const basic_string& __str);
    ^
/usr/bin/../lib/c++/v1/string:1141:5: note: candidate constructor not viable: cannot convert argument of incomplete type
      'char (*)[]' to 'std::__1::basic_string<char>'
    basic_string(basic_string&& __str)
    ^
/usr/bin/../lib/c++/v1/string:1167:5: note: candidate constructor not viable: cannot convert argument of incomplete type
      'char (*)[]' to 'initializer_list<value_type>'
    basic_string(initializer_list<value_type> __il);
    ^
/usr/bin/../lib/c++/v1/string:1161:9: note: candidate constructor template not viable: requires 2 arguments, but 1 was
      provided
        basic_string(_InputIterator __first, _InputIterator __last);
        ^
/usr/bin/../lib/c++/v1/string:1164:9: note: candidate constructor template not viable: requires 3 arguments, but 1 was
      provided
        basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
        ^
/usr/bin/../lib/c++/v1/string:1134:31: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    _LIBCPP_INLINE_VISIBILITY basic_string()
                              ^
/usr/bin/../lib/c++/v1/string:1138:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(const basic_string& __str, const allocator_type& __a);
    ^
/usr/bin/../lib/c++/v1/string:1144:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(basic_string&& __str, const allocator_type& __a);
    ^
/usr/bin/../lib/c++/v1/string:1148:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(const_pointer __s, const allocator_type& __a);
    ^
/usr/bin/../lib/c++/v1/string:1150:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(const_pointer __s, size_type __n);
    ^
/usr/bin/../lib/c++/v1/string:1152:5: note: candidate constructor not viable: requires 3 arguments, but 1 was provided
    basic_string(const_pointer __s, size_type __n, const allocator_type& __a);
    ^
/usr/bin/../lib/c++/v1/string:1154:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(size_type __n, value_type __c);
    ^
/usr/bin/../lib/c++/v1/string:1156:5: note: candidate constructor not viable: requires 3 arguments, but 1 was provided
    basic_string(size_type __n, value_type __c, const allocator_type& __a);
    ^
/usr/bin/../lib/c++/v1/string:1157:5: note: candidate constructor not viable: requires at least 2 arguments, but 1 was
      provided
    basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
    ^
/usr/bin/../lib/c++/v1/string:1169:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    basic_string(initializer_list<value_type> __il, const allocator_type& __a);

【问题讨论】:

  • cstr* 会给你一个char 指针数组。你想要std::list&lt;cstr&gt; 吗?

标签: c++


【解决方案1】:

您创建一个包含 10 个指向 char 的指针的列表,即,您的列表元素是 char** 类型,但您希望它们是 char*char const* 类型。请注意,修复类型将导致程序具有未定义的行为:使用 char const* 调用 std::string 构造函数的前提条件是指针指向有效的 C 字符串。

【讨论】:

    【解决方案2】:

    因为行:

    std::vector<std::string> vec(lict.begin(), lict.end());
    

    使用具有以下原型的构造函数(参见here):

    template< class InputIt >
    vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() );
    

    firstlast 是容器中的迭代器,其包含的类型必须与您的容器相同,即std::string

    【讨论】:

      【解决方案3】:

      候选构造函数不可行:无法将不完整类型“char (*)[]”的参数转换为“const_pointer”(又名“const char *”)

      列表的值类型不能转换为std::string

      试试const char*:

      std::list<const char*> lict{"Hello", "World"};
      std::vector<std::string> vec(lict.begin(), lict.end());
      

      here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2018-11-07
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多