问题 I: 零基础学C/C++188——编字典


这道题如果用普通的冒泡排序,插入排序,选择排序基本上都会时间超限,那不如用c++的sort,方便又快捷,既然都用了c++的,那么他的string定义字符串我们也可以学学

点击查看代码
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
string s[100010];//注意不要把数组开小了哦
int main()
{
	long long n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
	        cin>>s[i];
	}
	sort(s,s+n);
	for(int i=0;i<n;i++)
	{
		printf("%s",s[i].c_str());//因为用的是printf,他无法直接输出string类型的字符需要加上.c_str()强制转换。
                //cout<<s[i];这样子也是对的
		if(i!=n-1) printf(" \n");
	}
	return 0;
}

posted @ 2022-11-09 16:21  美羊羊给沸羊羊搬砖  阅读(142)  评论(0)    收藏  举报