string数组

来源:互联网 发布:泉州师范学院软件学院 编辑:程序博客网 时间:2024/05/21 09:35

其实没有string数组 要用就用vector<string>来替代

 #include<iostream>
#include<string>
#include<vector>
#include <stdio.h>
using namespace std;
int main()
{
int n;
int j = 0;
string s;
vector<string> str;
//string str[900];
 while(cin >> n)
     {
        for (int i = 0; i<n; i++)
{
cin >> s;
//str.at(i) = s; //error 越界
str.push_back(s);
j++;
}

// if(j==i)
//   break;


// getline(cin,s);
// str.at(j)=s;



int k;
cin >> k;
cout << str[str.size() - k-1] << endl;//从0开始 
     }


}

注意:
#include<iostream>
#include<string>
#include<vector>
#include <stdio.h>
using namespace std;
int main()
{
int n;
int j = 0;
string s;
vector<string> str ;
//string str[900];
cin >> n;

for (int i = 0; i<n; i++)
{
cin >> s;
//str[i] = s; //error 越界 覆盖了
//str.at(i) = s; //error 越界
str.push_back(s);//必须先压一个才可以用下标访问
str[i] = s; // 越界 覆盖了上面的 前面必须有push
str.at(i) = s; //覆盖了上面的  前面必须有push

j++;
}


// if(j==i)
//   break;



// getline(cin,s);
// str.at(j)=s;






int k;
cin >> k;
cout << str[str.size() - k-1] << endl;//从0开始
}

http://blog.csdn.net/cs_zlg/article/details/8171518 
http://zhidao.baidu.com/question/552958254.html 

0 0
原创粉丝点击