Jun 30, 2013 at 6:49pm UTC
I'm trying to take multiple lines of string to a vector, like "hello you", "hello world".
and then through for_each changes the ASCII value on all characters in the string including blank. to be the next ASCII.
It's here I seem to fail ( or I think).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string operator ()(string s)
{
for ( char &c : s )
{
cout<<c<<" " <<"changes till: " ;
c = {c+1};
cout<<c<<endl;
++sum_changes;
}
return s;
}
--------
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <cctype>
using namespace std;
template <typename T>
ostream& operator <<(ostream& os, const vector<T>& v)
{
copy(v.cbegin(), v.cend(), ostream_iterator<T>(os, "\n" ));
return os;
}
class scrambler
{
public :
scrambler (int init=0) : sum_changes(init), c2(init) {}
string operator ()(string s)
{
for ( char &c : s )
{
cout<<c<<" " <<"changes till: " ;
c = {c+1};
//c = toupper( c );
cout<<c<<endl;
++sum_changes;
}
return s;
}
int get_change()const {return sum_changes;}
private :
double sum_changes;
char c2;
string s2;
};
int main()
{
vector<string> v;
string in;
while (getline(cin,in,'\n' ))
{
v.push_back(in);
}
scrambler scramble;
for_each(v.begin(),v.end(),ref(scramble));
cout <<"String outputs after changes:\n " << v << endl;
cout<<"total changes done: " <<scramble.get_change()<<endl;
return 0;
}
Example run:
~/Desktop $ ./a.out
abc
a changes till: b
b changes till: c
c changes till: d
String outputs after changes:
abc
total changes done: 3
Last edited on Jun 30, 2013 at 6:50pm UTC
Jun 30, 2013 at 6:54pm UTC
Maybe you meant
string operator()(string &s )
?
Jun 30, 2013 at 7:02pm UTC
The change made it work. but i'm a bit confused because I thought the
Return s; would do the thing.
Jun 30, 2013 at 7:05pm UTC
You do not use the return value.
Jun 30, 2013 at 7:12pm UTC
I just realized it now, Thanks to you :)