演示string对象的例子

来源:百度知道 编辑:UC知道 时间:2024/06/17 11:38:29
#include <iosteam>;
#include <string>;
#include <algorithm>;
using namespace std;
int main(){
string str1="we are here!",str2=str1;
reverse(&str1[0],&str1[0]+12);
cout<<str1<<endl;
copy(&str1[0],&str1[0]+12,&str2[0]);
cout<<str2<<endl;
reverse_copy(&str2[0],&str2[0]+12,ostream_iterator<char>(cout));
}
程序运行出错,请帮忙调试
cout<<str1<<endl; //输出逆向后内容
copy(&str1[0],&str1[0]+12,&str2[0]); // 原样复制到str2
cout<<str2<<endl; //输出str2
reverse_copy(&str2[0],&str2[0]+12,ostream_iterator<char>(cout));//逆向输出str2
}

我这样是可以的
1.库后面不要加分号
2.是iostream,不是iosteam
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str1="we are here!",str2=str1;
reverse(&str1[0],&str1[0]+12);
cout<<str1<<endl;
copy(&str1[0],&str1[0]+12,&str2[0]);
cout<<str2<<endl;
reverse_copy(&str2[0],&str2[0]+12,ostream_iterator<char>(cout));
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str1="we are here!",str2=str1;
reverse(&str1[0],&str1[0]+12);
cout<<str1<<endl;
copy(&str1[0],&str1[0]+12,&str2[0]);
cout<<str2<<endl;
reverse_copy(&str2[0],&str2[0]+12,ostream_iterator<char>(cout));
}