簡體   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? 但我想您是在問如何更改數組元素的值。

要修改數組元素,可以將數組的索引分配給將數組元素更改為的元素。 但是,請記住,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;
}

如果要將Data作為DataStorage的類成員,則必須在成員初始化列表中對其進行初始化。 我也強烈建議對裸數組使用抽象,例如std::array 這允許通過at()函數使用邊界檢查訪問。 然后,您可以訪問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