其实没有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<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开始
}
#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