Dzięki @Azarien, o to mi chodziło :) Mam jednak problem z optymalnością programu
KOD 1:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
char *cstr, *p;
char arry[2] = {1,2};
string text;
while(getline(cin, text))
{
cstr = new char[text.size()+1];
strcpy (cstr, text.c_str());
p = cstr;
while(*p!=0)
{
switch(*p)
{
case 'X': printf("%c", 'A');
break;
case 'Y': printf("%c", 'B');
break;
case 'Z': printf("%c", 'C');
break;
case ' ': printf("%c", *p++);
break;
default: printf("%c", (char)(int)(*p++)+3);
}
}
cout<<endl;
}
return 0;
}
KOD2:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string text;
while(getline(cin, text)){
for(short i = 0; i<text.length(); i++)
{
switch(text[i])
{
case 'X': printf("%c", 'A');
break;
case 'Y': printf("%c", 'B');
break;
case 'Z': printf("%c", 'C');
break;
case ' ': printf("%c", (char)(int)text[i]);
break;
default: printf("%c", (char)(int)text[i]+3);
}
}
cout<<endl;
}
return 0;
}
Dlaczego KOD1 jest wolniejszy od KOD2, skoro KOD1 operuje na wskaźnikach?