C++ 结构数组

C++ 结构数组,c++,arrays,struct,C++,Arrays,Struct,我只想将这些值硬编码到一个表中。当我尝试使用2D数组时,我遇到了处理字符和整数的问题。当我做一个struct时,到目前为止我已经有了这个,但是它没有将信息分成列,我不知道如何以这种方式格式化它。(我一开始只做了3行,如果我让它们工作,其余的都一样) #包括 #包括 #包括 使用名称空间std; typedef结构表 { 字符串游戏; 国际年; 浮动评级; 选民人数; }t; void processTab(t*); int main() { t tabl[2]={0,0}; int i; 进程表

我只想将这些值硬编码到一个表中。当我尝试使用2D数组时,我遇到了处理字符和整数的问题。当我做一个struct时,到目前为止我已经有了这个,但是它没有将信息分成列,我不知道如何以这种方式格式化它。(我一开始只做了3行,如果我让它们工作,其余的都一样)

#包括
#包括
#包括
使用名称空间std;
typedef结构表
{
字符串游戏;
国际年;
浮动评级;
选民人数;
}t;
void processTab(t*);
int main()
{
t tabl[2]={0,0};
int i;
进程表(tabl);

因为(i=0;i我想你要找的是

避免
使用名称空间std;
,当您使用包含许多名称空间的复杂代码时,这几个字母是避免混淆的一个小代价。
避免
endl
:它刷新缓冲区,但速度很慢。如果您只需要换行,请使用
'\n'

此外,您还可以使用新的初始化语法来初始化列表:

std::vector<table> tbal = { 
                 {"twilight struggles  ", 2005, 8.226, 10690},
                 {"Agricola            ", 2007, 8.17 , 23738},
                 {"Puerto Rico         ", 2002, 8.163, 27433}
               };
std::vector tbal={
{《暮色之战》,2005年,8.226,10690},
{“Agricola”,2007年,8.1723738},
{“波多黎各”,2002年,8.163,27433}
};

首先,您需要正确定义您的
表(顺便说一下,
struct
的坏名称)。您试图使用
game
存储字符串,但只将其定义为单个
char
。您可能希望将其改为
std::string


然后,您可能希望在
操作符中进行格式化,简短回答是:使用
std::vector
,而不是原始数组

以下内容尽可能接近您的原始代码,为您介绍了最少数量的新功能:

#include <assert.h>         // assert
#include <iostream>         // std::cout, std::endl
#include <stddef.h>         // ptrdiff_t
#include <string>           // std::string
#include <utility>          // std::begin, std::end
#include <vector>           // std::vector
using namespace std;

typedef ptrdiff_t       Size;

template< class Container >
Size countOf( Container& c ) { return end( c ) - begin( c ); }

struct Game
{
    string  game;
    int     year;
    double  rating;
    int     num_voters;
};

void initialize( vector<Game>& games )
{
    assert( countOf( games ) == 0 );
    games.resize( 3 );

    games[0].game = "twilight struggles";
    games[0].year = 2005;
    games[0].rating = 8.226;
    games[0].num_voters = 10690;

    games[1].game = "Agricloa";
    games[1].year = 2007;
    games[1].rating = 8.17;
    games[1].num_voters = 23738;

    games[2].game = "Puerto Rico";
    games[2].year = 2002;
    games[2].rating = 8.163;
    games[2].num_voters = 27433;
}

int main()
{
    vector<Game> games;

    initialize( games );
    for( int i = 0; i < countOf( games ); ++i )
    {
        cout << i << endl;
        cout <<"\tGame: " << games[i].game << endl;
        cout<<"\tYear: " << games[i].year << endl;
        cout<<"\trating: " << games[i].rating << endl;
        cout<<"\tnum voters: " << games[i].num_voters << endl;
    }
}
#包含//断言
#包括//std::cout,std::endl
#包括//ptrdiff\t
#include//std::string
#包括//标准::开始,标准::结束
#include//std::vector
使用名称空间std;
typedef ptrdiff\t大小;
模板<类容器>
(容器和c){返回端(c)-开始(c);}的大小计数
结构游戏
{
弦乐游戏;
国际年;
双重评级;
选民人数;
};
无效初始化(向量和游戏)
{
断言(游戏计数=0);
游戏。调整大小(3);
游戏[0]。游戏=“暮光之城斗争”;
奥运会[0]。年份=2005年;
游戏[0],评分=8.226;
游戏[0]。投票人数=10690;
games[1]。game=“Agricloa”;
奥运会[1],2007年;
游戏[1]。评分=8.17;
游戏[1]。投票人数=23738;
games[2]。game=“波多黎各”;
奥运会[2]。年份=2002年;
游戏[2]。评分=8.163;
游戏[2]。投票人数=27433;
}
int main()
{
向量游戏;
初始化(游戏);
对于(int i=0;i你的问题是什么?(你的结构看起来像C代码,C++中,你只写代码>结构表{};< /Cord>)可能的错误:您的t.game的数据类型是char,您正在尝试将字符串复制到其中。可能的错误2:使用strcpy!对于
game
,您应该使用
std::string
,而不是
char
,除非
game
始终是单个字符。此外,
strcpy
行不应该编译,但您不会使用如果您使用了
std::string
(您只需使用
=
运算符),那么它们就是函数定义(在main之后)?避免使用命名空间std;
的建议是不好的。一个人必须将智慧应用到每件事上。有不好的环境,也有好的环境,初学者只访问后一类环境。但更重要的是,不应该被教导使用机械规则。应该被教导思考。第二,a考虑到代码的效率,Engl>Eng/Engult是不好的。效率,不要使用IOFFROW。也不要担心小东西。初学者不应该被教导去思考微效率。这是初学者意大利面最常见的原因。-1的“如果你有一个新的编译器”。,这是不正确的。自ARM(C++98之前)以来,聚合的初始化就得到了支持。@Cheersandhth.-Alf:我提到这一点是因为它直到最近才对
std::string
起作用。如果成员是
const char*
char[]
,那么你是对的,它在今年之前就已经工作了。@Cheersandhth.-Alf:
std::vector
不是聚合,在C++11之前不能用该语法初始化。
#include <string>
struct table
{
    std::string game;
    int year;
    double rating;
    int num_voters;
};
std::vector<table> tbal = { 
                 {"twilight struggles  ", 2005, 8.226, 10690},
                 {"Agricola            ", 2007, 8.17 , 23738},
                 {"Puerto Rico         ", 2002, 8.163, 27433}
               };
std::ostream &operator<<(std::ostream &os, table const &t) { 
    os << "Game: " << setw(20) << t.game;
    os << "\tYear: " << setw(4) << t.year;
    os << "\tRating: " << fixed << setprecision(2) << t.rating;
    return os << "\tVoters: " << setw(6) << t.num_voters;    
}
std::vector<tabl> tabl;
std::copy(tabl.begin(), tabl.end(), std::ostream_iterator<table>(std::cout, "\n"));
#include <assert.h>         // assert
#include <iostream>         // std::cout, std::endl
#include <stddef.h>         // ptrdiff_t
#include <string>           // std::string
#include <utility>          // std::begin, std::end
#include <vector>           // std::vector
using namespace std;

typedef ptrdiff_t       Size;

template< class Container >
Size countOf( Container& c ) { return end( c ) - begin( c ); }

struct Game
{
    string  game;
    int     year;
    double  rating;
    int     num_voters;
};

void initialize( vector<Game>& games )
{
    assert( countOf( games ) == 0 );
    games.resize( 3 );

    games[0].game = "twilight struggles";
    games[0].year = 2005;
    games[0].rating = 8.226;
    games[0].num_voters = 10690;

    games[1].game = "Agricloa";
    games[1].year = 2007;
    games[1].rating = 8.17;
    games[1].num_voters = 23738;

    games[2].game = "Puerto Rico";
    games[2].year = 2002;
    games[2].rating = 8.163;
    games[2].num_voters = 27433;
}

int main()
{
    vector<Game> games;

    initialize( games );
    for( int i = 0; i < countOf( games ); ++i )
    {
        cout << i << endl;
        cout <<"\tGame: " << games[i].game << endl;
        cout<<"\tYear: " << games[i].year << endl;
        cout<<"\trating: " << games[i].rating << endl;
        cout<<"\tnum voters: " << games[i].num_voters << endl;
    }
}