Jan 29, 2014 at 12:36pm UTC
You could do it by keeping a separate container of sorted strings. It's not too bad in terms of efficiency, since it's a pretty small exercise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#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 );
}
}
for ( const auto &i : lines )
out << i << std::endl;
in.close();
out.close();
return 0;
}
output.txt
stack
stacka
overflow
ooverflow
Last edited on Jan 29, 2014 at 2:08pm UTC
Jan 29, 2014 at 12:56pm UTC
aydo000 wrote:i am using dev c++
That's why. You'll need a C++11 compliant compiler for that line to work. Otherwise, you can use iterators.
Last edited on Jan 29, 2014 at 12:56pm UTC
Jan 30, 2014 at 7:54am UTC
That's why. You' ll need a C++11 compliant compiler for that line to work. Otherwise, you can use iterators.
if i want to combine this programs.for example
input.txt
stack
kcats
stacka
astack
kcats
overflow
ooverflow
overflowo
Abc
ABC
output.txt
stack
stacka
overflow
ooverflow
Abc
how can we write ?