{
"question_id": 237827, "question_title": "字符串排序", "question_content": "编写程序,输入若干个字符串。\n要求:\n(1)按字符串长度的大小升序输出各个字符串。\n(2)按字符串中字符的ASCII码值大小升序输出各个字符串。", "difficulty": "简单", "answer_id": 218781, "answer_content": "如果按照 1条件优先于2的话,我会这么写。。(你作业好多呀)\n\n```\n #include <string>\n#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\n\n//比较函数,用于排序\nbool compare(string a,string b) {\n //长度不一样的时候采用长度来排序\n if (a.length() != b.length()) {\n return a.length() < b.length();\n }\n //长度一样的时候采用ASCII值排序\n return a < b;\n}\n\nint main()\n{\n \n vector<string>list;\n \n string inputString;\n \n while (cin>>inputString) {\n \n //结束标志,测试方便,可以注释掉\n if (inputString == \"0\") {\n break;\n }\n //加入到vector\n list.push_back(inputString);\n }\n //排序,系统方法\n sort(list.begin(),list.end(),compare);\n //依次输出\n for (int i=0; i<list.size(); i++) {\n cout<<list[i]<<endl;\n }\n \n return 0;\n}\n```\n\n", "tag_name": "c++", "cpp": "#include <string>\n#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\nbool compare(string a,string b) {\n\tif (a.length() != b.length()) {\n\t return a.length() < b.length();\n\t}\n\treturn a < b;\n}\nint main()\n{\n\tvector<string>list;\n\tstring inputString;\n\twhile (cin>>inputString) {\n\t\tif (inputString == \"0\") {\n\t\t\tbreak;\n\t\t}\n\t\tlist.push_back(inputString);\n\t}\n\tsort(list.begin(),list.end(),compare);\n\tfor (int i=0; i<list.size(); i++) {\n\t\tcout<<list[i]<<endl;\n\t}\n\treturn 0;\n}", "topic_link": "https://bbs.csdn.net/topics/600470184", "status": 1, "keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配", "license": "csdn.net", "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/140.ipynb?type=file" }, "notebook_enable": 1, "author": "likaiyihou512"
}