带有字符串数组的C 列表
C++ list with array of strings
我必须与人一起列出列表,他们的考试结果在C 中。我的问题是我不知道如何输入数组。我试图制作3D字符串,但这行不通。它必须在功能中!如果您有更好的建议,我将非常感激
我的输入一定是:
Peter Evens 4.86
*其他人**
*小组的平均结果**
*最高级的人**
*最低等级的那个
这就是我现在所做的:
#include <iostream>
#define MAXN 200
#define MAXM 200
#define MAXX 200
using namespace std;
void input(char list[][MAXN][MAXM], int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
for (int p = 0; p < n; p++)
cin >> list[i][j][p];
}
}
int main() {
char list[31][MAXN][MAXM];
int n;
cin >> n;
input(list, n);
return 0;
}
您不需要运行第三个迭代,因为您不必按字符存储输入字符。第三维可用于存储整个字符串。
尝试以下方式。观察我在您的片段中进行的更正。
#include <iostream>
#define MAXN 200
#define MAXM 200
#define MAXX 200
using namespace std;
void input(char list[][3][MAXM], int n) {
for (int i = 0; i < n; i++) //number of entries
for (int j = 0; j < 3; j++) { //3 fields - fname, lname and marks
cin >> list[i][j];
}
}
int main() {
char list[31][3][MAXM];
int n;
cin >> n;
input(list, n);
return 0;
}
您必须使用struct的向量。
以这种方式定义您的结构:
typedef struct details{
string Fname;
string Lname;
float marks;
} details;
和向量应该看起来像
vector< details > info;
我只是向您展示如何在以下代码中实现此目标:
void insertValues(vector< details >& info){
int n;
details d;
cout<<"Enter the size of record: ";
cin>>n;
cout<<"Enter records: "<<endl;
for(int i=0;i<n;i++){
cin>>d.Fname>>d.Lname>>d.marks;
info.push_back(d);
}
}
void LowestScorer(vector< details > info){
details d;
vector< details >::iterator it=info.begin();
d=*it;
for(;it!=info.end();it++){
if(d.marks > it->marks){
d=*it;
}
}
cout<<"Lowest Scorer: "<<d.Fname<<" "<<d.Lname<<" "<<d.marks<<endl;
}
主看起来像这样:
int main(){
vector< details > info;
insertValues(info);
LowestScorer(info);
return 0;
}
如上所述,现在是学习vector
的好时机。这很容易。以下是一个非常简单且整洁的解决方案:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<string, string> > Container;
int main()
{
Container container;
container.push_back(make_pair("Peter Evens", "4.86"));
container.push_back(make_pair("Other name", "his score"));
return 0;
}
相关文章:
- C++如何生成std::数组列表
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- GTEST : 如何将数组列表作为参数传递到 ValuesIn of INSTANTIATE_TEST_CASE_P?
- 基于数组的列表 - 动态数组创建时出错
- 如何使用列表/数组在C++中实现 Python "... not in ..."?
- 从非托管 C# DLL 返回列表/数组
- C++对象列表数组
- 如何在C++中创建配对列表数组
- 链接列表数组
- 获取列表数组中的所有列表项
- 如何创建和扫描列表/数组..在嵌入式剪辑中
- 列表数组<string>,插入字符串行为奇怪
- 对 STL 列表数组使用迭代器的语法
- 在类|C++中创建分数列表数组
- C++是否有一些类似C#类型的东西来存储列表/数组中的类类型
- 列表数组-构造函数和main()中的地址不匹配
- c++中的列表数组.指针;/
- STL 列表数组无法访问列表函数
- 用动态内存管理创建类列表数组