|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- #include<cstring>
- using namespace std;
- string str[12];
- int main()
- {
- for(int i=1;i<=10;++i)
- {
- gets(str[i]);
- }
-
- for(int i=1;i<10;++i)
- {
- for(int j=1;j<=10-i;++j)
- {
- if(strcmp(str[i],str[i+j])>0)
- {
- string temp=str[i];
- str[i]=str[i+1];
- str[i+1]=temp;
- }
- }
- }
-
- for(int i=1;i<=10;++i)
- {
- cout<<str[i]<<endl;
- }
-
- return 0;
- }
复制代码
代码如上,编译后显示错误信息:[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
请问应该如何改正
本帖最后由 jackz007 于 2024-3-9 16:56 编辑
【代码1】:
- #include<iostream>
- #include<cstring>
- using namespace std ;
- int main(void)
- {
- char str[10][64] , temp[64] ;
- for(int i = 0 ; i < 10 ; i ++) gets(str[i]) ;
- for(int i = 0 ; i < 10 - 1 ; i ++) {
- for(int j = i + 1 ; j < 10 ; j ++) {
- if(strcmp(str[i] , str[j]) > 0) {
- strcpy(temp , str[i]) ;
- strcpy(str[i] , str[j]) ;
- strcpy(str[j] , temp) ;
- }
- }
- }
- for(int i = 0 ; i < 10 ; i ++) cout << str[i] << endl ;
- }
复制代码
【代码2】:
- #include<iostream>
- #include<cstring>
- using namespace std ;
- int main(void)
- {
- string str[10] ;
- for(int i = 0 ; i < 10 ; i ++) cin >> str[i] ;
- for(int i = 0 ; i < 10 - 1 ; i ++) {
- for(int j = i + 1 ; j < 10 ; j ++) {
- if(str[i] . compare(str[j]) > 0) {
- string temp = str[i] ;
- str[i] = str[j] ;
- str[j] = temp ;
- }
- }
- }
- for(int i = 0 ; i < 10 ; i ++) cout << str[i] << endl ;
- }
复制代码
|
|