简体   繁体   English

如何调用数组中的值并更改数组中的值?

[英]How do I call the value in array and change the value in array?

class DataStorage{
                        // 0 1 2 3 4        5 6 7 8
string Data[20][4]={{"Wee","50","1","First"},{"Wee","22","2","First"},
                        // 9 10 11 12       13 14 15 16
                    {"Jason","26","3","First"},{"Krappa","12","4","First"},
                        // 17 18 19 20      21 22 23 24
                    {" "," ","5","First"},{" "," ","6","Economy"},
                        //25 26 27 28       29 30 31 32
                    {"Kappa","15","7","Economy"},{"Eraser","17","8","Economy"},
                        //33 34 35 36       37 38 39 40
                    {" "," ","9","Economy"},{"Morty"," ","10","Economy"},
                        //41 42 43 44       45 46 47 48
                    {"Rick"," ","11","Economy"},{"Amanda","10","12","Economy"},
                        //49 50 51 52       53 54 55 56
                    {"Lee","","13","Economy"},{"MingLee"," ","14","Economy"},
                        //57 58 59 60       61 62 63 64
                    {"Beauty"," ","15","Economy"},{"S4head"," ","16","Economy"},
                        //65 66 67 68       69 70 71 72
                    {"Ivan"," ","17","Economy"},{"Dex"," ","18","Economy"},
                        //73 74 75 76       77 78 79 80
                    {"Chua"," ","19","Economy"},{"Haha"," ","20","Economy"},};
};
int main(){

}

How do I call the value in array and change the value in array? 如何调用数组中的值并更改数组中的值? Do I need to make some function to get value from the input and pass it into a variable in class and set it into my array? 我是否需要做一些函数来从输入中获取值,并将其传递给类中的变量,然后将其设置为数组?

I'm not sure what you're asking when you say How do I call the value in array and change the value in array? 我不确定您在说什么时要问的问题How do I call the value in array and change the value in array? but I think you're asking how do you change the value of an array element. 但我想您是在问如何更改数组元素的值。

To modify an array element you assign the array's index to what you're changing the array's element to; 要修改数组元素,可以将数组的索引分配给将数组元素更改为的元素。 however, remember that C++ arrays are 0-index arrays meaning when you start counting their elements at 0. For example the following code modifies the element at index 5. Live preview 但是,请记住,C ++数组是0索引数组,这意味着当您开始将其元素计数为0时。例如,以下代码修改了索引5处的元素。 实时预览

#include <iostream>

int array[10] = {1, 5, 33, 7, -23, 2, 8, 54, 19, 2};

int main() {
  std::cout << array[5] << std::endl;

  array[5] = 100; // Set the value of the element at index 5 to 100

  std::cout << array[5] << std::endl;

  return 0;
}

If you want to have Data as a class member of DataStorage you have to initialize it in the member initialization list. 如果要将Data作为DataStorage的类成员,则必须在成员初始化列表中对其进行初始化。 I also highly recommend to use an abstraction for the bare array, like std::array . 我也强烈建议对裸数组使用抽象,例如std::array This allows to use bounds-checked access with the at() function. 这允许通过at()函数使用边界检查访问。 You can then access Data and change it's contents. 然后,您可以访问Data并更改其内容。

#include <array>
#include <iostream>
#include <string>

class DataStorage
{
public:
    std::array<std::array<std::string,4>,20> Data;
    DataStorage() : Data({{
                {{"Wee","50","1","First"}},
                {{"Wee","22","2","First"}},
                {{"Jason","26","3","First"}},
                {{"Krappa","12","4","First"}},
                {{" "," ","5","First"}},
                {{" "," ","6","Economy"}},
                {{"Kappa","15","7","Economy"}},
                {{"Eraser","17","8","Economy"}},
                {{" "," ","9","Economy"}},
                {{"Morty"," ","10","Economy"}},
                {{"Rick"," ","11","Economy"}},
                {{"Amanda","10","12","Economy"}},
                {{"Lee","","13","Economy"}},
                {{"MingLee"," ","14","Economy"}},
                {{"Beauty"," ","15","Economy"}},
                {{"S4head"," ","16","Economy"}},
                {{"Ivan"," ","17","Economy"}},
                {{"Dex"," ","18","Economy"}},
                {{"Chua"," ","19","Economy"}},
                {{"Haha"," ","20","Economy"}}
            }}) {}
};

int main()
{
    DataStorage d;
    std::cout << d.Data.at(10).at(2) << '\n'; // prints 11
    d.Data.at(10).at(2) = "1729";
    std::cout << d.Data.at(10).at(2) << '\n'; // prints 1729
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM