简体   繁体   English

如何在一维数组的每个元素中都有多个 int 值?

[英]How to have multiple int values in every element in a one dimensional array?

i want to fill one one dimensional array with two int values.我想用两个 int 值填充一个一维数组。

When i run my code without brackets around the values i get only the frist int and when i run it with brackets around the values i get only the second int.当我在值周围不带括号运行代码时,我只得到第一个 int,当我在值周围用括号运行它时,我只得到第二个 int。

Output should be like x/y Output 应该像 x/y

thank you for your time.感谢您的时间。

int main()
{
    cBruch cBruch[7];

    cBruch[0] = (3, 4);
    cBruch[1] = (24, -6);
    cBruch[2] = -5, -3;
    cBruch[3] = -14, 22;
    cBruch[4] = 21, 45;
    cBruch[5] = 7, -9;
    cBruch[6] = 2, 3;

    for (int i = 0; i < 7; i++)
    {
        cBruch[i].ausgabe();
    }

    return 0;
}

.h 。H

class cBruch
{
private:
    int zaehler;
    int nenner;

    int ggt(int a, int b);
    void kuerzen();

public:
    cBruch(int zaehler_in = 0, int nenner_in = 1);
    ~cBruch();

    void ausgabe(); //output
};

.cpp .cpp

cBruch::cBruch(int zaehler_in, int nenner_in)
{
    zaehler = zaehler_in;
    nenner = nenner_in;

    cout << "Konstruktor Zaehler = " << zaehler << " Nenner = " << nenner << endl;
}

cBruch::~cBruch()
{
    cout << "Destruktor Zaehler = " << zaehler << " Nenner = " << nenner << endl;
}

void cBruch::ausgabe()
{
    cout << "Bruch: " << zaehler << "/" << nenner << "\tGleitkommazahl: "<< double(zaehler) / nenner << endl;
}

You should initialize your array like following:你应该像下面这样初始化你的数组:

cBruch cBruch[] = {{3, 4},{24, -6},{-5, -3},{-14, 22},{21, 45},{7, -9},{2, 3}};

The comma operator will always evaluate both sides and return the right hand side. 逗号运算符将始终评估双方并返回右侧。 It also has the lowest precedence of all opearators.它的 优先级也是所有运算符中最低的。

So, this:所以这:

cBruch[2] = -5, -3;

evaluates to:评估为:

(cBruch[2] = -5), -3;

And this:和这个:

cBruch[0] = (3, 4);

Will evaluate to:将评估为:

cBruch[0] = 4;

You probably should use an array (or better yet a std::array ) of for example std::pair<int, int> .您可能应该使用例如std::pair<int, int>的数组(或者更好的是std::array )。

Apparently I can't read... You need {} rather than ().显然我看不懂...您需要 {} 而不是 ()。 – HolyBlackCat – 圣黑猫

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速从int **获得一维int数组? - How to quickly get one dimensional int array from int **? 为什么分配的二维int数组的存储地址序列与普通的数组不同? - Why does an allocated 2-dimensional int array have a different sequence of memory addresses as a normal one? 是否可以有一个带有int值和int引用的数组? - Is it possible to have an array with int values and int references? 从一维向量复制<int>从二维向量对向量的第一个元素开始<pair<int,int> &gt;矩阵 - Copying from one dimensional vector vector<int> starts to first element of two dimensional vector pair vector<pair<int,int>>matrix 难以找到二维数组中每一列的最大值 - Trouble finding maximum values for every column in 2 dimensional array C++如何在n个元素的一维数组中找到最小值和最大值? - C++ How to find min and max values in a one-dimensional array of n elements? 从两列之一返回值,还是跳过数组中的所有其他元素? - Return values from one of two columns, or skip every other element in an array? 如何在C ++中为二维数组赋值 - How to assign values to 2 dimensional array in c++ 如何在OpenCV中为3维数组分配值 - How to assign values to a 3 dimensional array in opencv 如何在C ++中找出二维int数组的大小? - How to find out the size of two dimensional an int array in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM