Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try:</p> <pre><code>int main() { std::deque&lt;std::string&gt; mydeque; std::ifstream myifstream("test.txt"); std::string line; while(std::getline(myifstream, line) { mydeque.push_back(line); } } </code></pre> <p>If it is one word per line you can simplify to:</p> <pre><code>int main() { std::deque&lt;std::string&gt; mydeque; std::ifstream myifstream("test.txt"); // Note: istream_iterator&lt;T&gt; uses std::istream&amp; operator&gt;&gt;(std::istream&amp;, T&amp;) to // read data from the stream. If `T` is a std::string this means it will // read a single space separated word. std::copy(std::istream_iterator&lt;std::string&gt;(myifstream), std::istream_iterator&lt;std::string&gt;(), std::back_inserter(mydeque) ); } </code></pre> <p>If each line contains multiple words and you want to use the back inserter then you need to define a class for reading a whole line in an object that can be used with iterators:</p> <pre><code>struct Line { std::string data; operator std::string const&amp;() const {return data;} friend std::istream&amp; operator&gt;&gt;(std::istream&amp; s, Line&amp; dst) { return std::getline(s, dst.data); } }; int main() { std::deque&lt;std::string&gt; mydeque; std::ifstream myifstream("test.txt"); std::copy(std::istream_iterator&lt;Line&gt;(myifstream), std::istream_iterator&lt;Line&gt;(), std::back_inserter(mydeque) ); } </code></pre> <p>Or we can just use the constructrs:</p> <pre><code>int main() { std::ifstream myifstream("test.txt"); std::deque&lt;std::string&gt; mydeque(std::istream_iterator&lt;Line&gt;(myifstream), (std::istream_iterator&lt;Line&gt;())); // Note: Extra brace required around second iterator here // This is to avoid the problem with the `Most Vexing Parse` // Which would otherwise make this a function declaration } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload