Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:09:41+00:00 2026-05-31T07:09:41+00:00

int main() { std::deque<std::string> mydeque; std::back_insert_iterator<decltype(mydeque)> myback_insert_iterator(mydeque); std::ifstream myifstream(test.txt); while(std::getline(myifstream, *myback_insert_iterator)) { } }

  • 0
int main() { 
    std::deque<std::string> mydeque;
    std::back_insert_iterator<decltype(mydeque)> myback_insert_iterator(mydeque);
    std::ifstream myifstream("test.txt");
    while(std::getline(myifstream, *myback_insert_iterator)) {
    }
}

I simply want to read line-wise a text file into a string container.
This produces compiler error:

C2784: could not deduce template argument for ‘std::basic_istream<_Elem,_Traits> &’ from ‘std::ifstream’

What’s wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:09:43+00:00Added an answer on May 31, 2026 at 7:09 am

    Try:

    int main()
    {
        std::deque<std::string> mydeque;
        std::ifstream myifstream("test.txt");
    
        std::string line;
        while(std::getline(myifstream, line)
        {
            mydeque.push_back(line);
        }
    }
    

    If it is one word per line you can simplify to:

    int main()
    {
        std::deque<std::string> mydeque;
        std::ifstream myifstream("test.txt");
    
        // Note: istream_iterator<T> uses std::istream& operator>>(std::istream&, T&) 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<std::string>(myifstream),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(mydeque)
                 );
    }
    

    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:

    struct Line
    {
        std::string data;
        operator std::string const&() const {return data;}
    
        friend std::istream& operator>>(std::istream& s, Line& dst)
        {
            return std::getline(s, dst.data);
        }
    };
    
    int main()
    {
        std::deque<std::string> mydeque;
        std::ifstream myifstream("test.txt");
    
        std::copy(std::istream_iterator<Line>(myifstream),
                  std::istream_iterator<Line>(),
                  std::back_inserter(mydeque)
                 );
    }
    

    Or we can just use the constructrs:

    int main()
    {
        std::ifstream myifstream("test.txt");
        std::deque<std::string> mydeque(std::istream_iterator<Line>(myifstream),
                                        (std::istream_iterator<Line>()));
        // 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
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <fstream> int main() { std::ofstream outfile(text.txt, ios::trunc); std::ifstream infile(text.txt, ios::trunc); outfile.seekp(0);
#include <iostream> #include <fstream> int main() { std::fstream inf( ex.txt, std::ios::in ); while( !inf.eof()
I created a program called test: #include<stdlib.h> #include<iostream> int main() { std::cout<<system(..\\add\\debug\\add.exe 4 8);
int main(void) { std::string foo(foo); } My understanding is that the above code uses
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have this code, int main() { std::string st; std::stringstream ss; ss<<hej hej med
I have this puny program. #include <iostream> #include <string> int main() { std::string st
For example: #include <stdio.h> #include <string> int main() { std::string* stuff(NULL); printf(allocating memory...); //line
In the following program: int main(){ std::cout<<enter numbers to be divide<<std::endl; int a,b,c; while(true){
//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.