0
for( const auto &i : lines )
        out << i << std::endl;

this code is not working on VS10. how can i change it with iterators ?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

#define find_str( a, b ) std::find( a.begin(), a.end(), b ) == a.end() 

int main( int argc, const char* argv[] )
{
    std::ifstream in( "input.txt" );
    std::ofstream out( "output.txt" );
    std::vector<std::string> lines, sorted_lines;

    if( !in )
    {
        std::cerr << "Error: Couldn't open file input.txt";
        return -1;
    }

    std::string str, sorted_str;

    while( in )
    {
        std::getline( in, str );
        sorted_str = str;
        std::sort( sorted_str.begin(), sorted_str.end() );

        if( find_str( sorted_lines, sorted_str ) )
        {
            lines.push_back( str );
            sorted_lines.push_back( sorted_str );
        }
    }
    const auto i;
    for( const auto &i : lines )
        out << i << std::endl;

    in.close();
    out.close();

    return 0;
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
aydo000
  • 37
  • 4
  • What is "C++10"? There is no such standard. I guess you mean "pre C++11" (a.k.a C++03)? – Some programmer dude Jan 30 '14 at 07:31
  • @JoachimPileborg I guess that means "C++ as VS2010 supports it." Which is a mix of C++03, C++11 and vendor-specifics. – Angew is no longer proud of SO Jan 30 '14 at 07:31
  • 1
    As for your problems, you *have* looped with iterators before, haven't you? If not then you need to find a [good tutorial or basic book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Jan 30 '14 at 07:31
  • @JoachimPileborg yea. i mean pre C++11 – aydo000 Jan 30 '14 at 07:32
  • @JoachimPileborg i can't compile this code with VS2010. i want to change with iterators. – aydo000 Jan 30 '14 at 07:33
  • 1
    Also, do do e.g. `while (in)`, it will not work exactly as you expect it to. Instead do `while (std::getline(...))`. Reason being that the `eof` flag is not set until *after* you try to read from beyond the end of the file. So in your loop your `std::getline` call will fail, you will not notice it and use `str` even though it haven't changed, and *then* the loop will end. – Some programmer dude Jan 30 '14 at 07:33
  • This code won't compile anywhere, let alone VS2010. `const auto i;` is illegal, can't use `auto` type deduction in the absence of an initializer. – Praetorian Jan 30 '14 at 07:37
  • @excuse me. but i am not a good english speaker. honestly i didn't understand. can u change in code and write ? – aydo000 Jan 30 '14 at 07:40
  • @where should i initializer ? can u write ? – aydo000 Jan 30 '14 at 07:42

1 Answers1

0

It's a straightforward iterator-based loop:

for (auto it = begin(lines), itEnd = end(lines); it != itEnd; ++it)
  out << *it << std::endl;

If the compiler happens to botch argument-dependent lookup (not sure how good VS2010 is with it), replace begin and end with std::begin and std::end.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455