带有字符串数组的C 列表

C++ list with array of strings

本文关键字:列表 数组 字符串      更新时间:2023-10-16

我必须与人一起列出列表,他们的考试结果在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;
}