scanf函数和cin的区别、类的数组、C++排序函数
生活随笔
收集整理的這篇文章主要介紹了
scanf函数和cin的区别、类的数组、C++排序函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
給定n個字符串,將這n個字符串按照字典序進(jìn)行排列,此處用排列函數(shù)是C++的庫函數(shù)sort,產(chǎn)生如下兩個疑問,望大佬解答
#include <iostream> #include <algorithm> #include <string> #include <vector>using namespace std; /* ********************************************** Q1:為什么定義類的數(shù)組,無法用sort函數(shù)排序呢? 會出現(xiàn)數(shù)組越界的情況,求解答 ********************************************* int main() {string str[1000];int n;scanf("%d",&n);for(int i = 0;i < n;i++){scanf("%s",str+i);str[i] = str[i] +'\0';}sort(str,str+n-1);for(int i =0;i < n;i++)printf("%s\n",str+i);return 0; } ********************************************** */ /* 以下代碼正常運行 */ int main() {vector<string> str;string newstr;int n;scanf("%d",&n);for(int i = 0;i < n;i++){cin>>newstr;//scanf("%s",newstr); //Q2:這里用scanf進(jìn)行讀取輸入,為什么會出錯呢? str.push_back(newstr);}sort(str.begin(),str.end());for(int i =0;i < n;i++)cout<<str[i]<<endl;return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/qiang-wei/p/9363469.html
總結(jié)
以上是生活随笔為你收集整理的scanf函数和cin的区别、类的数组、C++排序函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL开发规范
- 下一篇: leetcode 58. Length