码迷,mamicode.com
首页 > 编程语言 > 详细

字符串排成字典序,字符串数组

时间:2015-09-10 09:36:51      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

字符串数组好久没用都忘了,定义string s[100];但是输入的时候如果写 scanf("%s", s[i]);就错了,提示

技术分享

也许%s只能把定义的char型的字符数组当成字符串输出,改成  cin>>s[i];就对了

 

而且strlen(s[i])都用不了,会提示

技术分享

而strlen括号里面是放字符串的所以我猜测用定义的string类型的数组时能用%s表示它的格式

我也不知道反正是猜测,改成cin>>s[i];就对了,还是c++方便,不用管格式

 

还有一个知识点就是,如果对定义的字符串数组string s[100]按照字典序排序,可以用sort(s, s+n);

#include<stdio.h>
#include <iostream>
#include<algorithm>
#include<string>

using namespace std;
string s[100];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        cin>>s[i];
    sort(s, s+n);
    for(int i = 0; i < n; i++)
        cout<<s[i]<<endl;
    int len1 = sizeof(s[0]);
    int len2 = sizeof(s[n-1]);
    printf("%d %d\n", len1, len2);
    return 0;
}

 

strlen -- 是函数,计算字符串长度 char s[]="010\010\\010\n";
010 -- 3个
\010 -- 1个 (八进制数)
\\ -- 1个
010 -- 3个
\n -- 1个
printf("%d",sizeof(s)); 得 10
 

字符串排成字典序,字符串数组

标签:

原文地址:http://www.cnblogs.com/rain-1/p/4796733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!