title: Using push_back on a vector of strings - C++
body: <p>I am trying to use <code>push_back</code> on a vector of strings in C++. How can I push a single character on to the vector? Currently, I have tried the following, all without success:  </p>
  
  <ol>
  <li>Initialized a string (tried to) with the character.  </li>
  </ol>
  
  <p>Code</p>
  
  <pre><code>string str(main_string[0]);  
  vector_string.push_back(str);
  </code></pre>
  
  <ol start="2">
  <li>Tried to invoke strcpy and thus copy contents. Const-ness seems to get in the way.  </li>
  </ol>
  
  <p>Code</p>
  
  <pre><code>string str;  
  strcpy(main_string[0], str.c_str());  
  vector_string.push_back(str);  
  </code></pre>
  
  <p>Any more suggestions/ideas are most welcome.  </p>
  
  <p>Edit: The error logs are as follows:  </p>
  
  <pre><code>test_push.C: In function ‘void test_push(std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;&amp;, std::string)’:
  test_push.C:50: error: no matching function for call to ‘std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char&amp;)’
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, _CharT, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const _CharT*, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;] &lt;near match&gt;
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const _CharT*, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:194: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:184: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:170: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:178: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.h:2147: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string() [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  test_push.C:61: error: conversion from ‘char’ to non-scalar type ‘std::string’ requested
  test_push:90: error: no matching function for call to ‘std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char&amp;)’
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, _CharT, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const _CharT*, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;] &lt;near match&gt;
  /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::basic_string(const _CharT*, typename _Alloc::rebind&lt;_CharT&gt;::other::size_type, const _Alloc&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]  
  </code></pre>
  
  <p>More info: In this function, either the entire string or just the first letter is pushed in to the vector based on its contents. So, in a way, I am looking for some function that converts a <code>char</code> into a C++ style <code>std::string</code> (to make it acceptable to a vector).</p>
  
  <p>Thanks,<br>
  Sriram</p>