| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1563 人关注过本帖
标题:C++的string 类到底要怎么用? 出现strcmp 2664错误 要怎么解?
只看楼主 加入收藏
wingc
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-3-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
C++的string 类到底要怎么用? 出现strcmp 2664错误 要怎么解?
我自己写的代码,但不会解这个错误,求大神帮忙,谢谢啦!

#include<iostream>
#include<string>
using namespace std;
void main()     //找出输入到数组A中的最长和最短的字符串,并求平均长度且A要动态申请空间并最后释放。
{ int n,i,j,max,min,sum=0,avg;
string *A;
cout<<"输入一个整数n:"<<endl;
cin>>n;
A=new string[n];    //题目要求要动态分配空间
cout<<"输入"<<n<<"个字符串"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{ if(strcmp(A[i],A[j])>0)
       max=i;  //记录下最长串的数组下标
  else min=i;  //记录下最短串的数组下标
}
cout<<"最长的串为:"<<A[max]<<"\n最短的串为:"<<A[min]<<endl;
cout<<"n个字符串的平均长度为:"<<(sizeof(A)-n)/n<<endl;  //这个我也不知道算法对不对
delete []A;  //释放空间
}
搜索更多相关主题的帖子: 错误 for string cout strcmp 
2020-03-10 22:19
gzy444
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2020-1-16
收藏
得分:7 
应该是strcmp函数只能比较char值,其他的好像都不行
2020-03-10 23:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:7 
题目让你“最和最的字符串”,你用 strcmp 是想求最的字符串?
(sizeof(A)-n)/n ------ 这个 sizeof(A) 又是什么?

程序代码:
#include <iostream>
#include <string>
using namespace std;

int main( void )
{
    size_t n;
    cin >> n;

    string* a = new string[n];
    {
        size_t longest_idx = 0;
        size_t shortest_idx = 0;
        size_t sum_length = 0;
        for( size_t i=0; i!=n; ++i )
        {
            cin >> a[i];

            if( a[longest_idx].size() < a[i].size() )
                longest_idx = i;

            if( a[shortest_idx].size() > a[i].size() )
                shortest_idx = i;

            sum_length += a[i].size();
        }
        cout << "the longest string is: " << a[longest_idx] << '\n';
        cout << "the shortest string is: " << a[shortest_idx] << '\n';
        cout << "the average length is: " << sum_length*1.0/n << '\n';
    }
    delete[] a;

    return 0;
}

输入
3
abc
abcdefghijk
abcdef
输出
the longest string is: abcdefghijk
the shortest string is: abc
the average length is: 6.66667

2020-03-11 09:06
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:7 
通过转换试看还有错误提示?
                if (strcmp((char*)&A[i], (char*)&A[j])>0)
2020-03-11 09:25
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下是引用xianfajushi在2020-3-11 09:25:28的发言:

通过转换试看还有错误提示?
 
                if (strcmp((char*)&A, (char*)&A[j])>0)

如果是比较大小,std::string 可直接使用 operator >,也就是 if(strcmp(A[i],A[j])>0) 改为 if( A[i] > A[j] )
如果一定要用 strcmp,那应该是 if( strcmp(A[i].c_str(),A[j].c_str()) > 0 )
2020-03-11 10:00
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:0 
strlen(A[0].data())
2020-03-11 14:53
快速回复:C++的string 类到底要怎么用? 出现strcmp 2664错误 要怎么解?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016496 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved