Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the ad-inserter domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rehub-framework domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-includes/functions.php on line 6131

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
[👨‍💻🇻🇳] Cấp phát động cho mảng hai chiều trong C++ - top1brand
Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

[👨‍💻🇻🇳] Cấp phát động cho mảng hai chiều trong C++


Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
class="post-inner post post-307931 type-post status-publish format-standard has-post-thumbnail hentry category-top1dev-no1dev category-top1index-top1list-top1world category-top1labs-no1labs category-top1vietnam-no1vietnam tag-no1dev tag-no1labs tag-top1dev tag-top1labs tag-cap tag-chieu tag-cho tag-dong tag-hai tag-mang tag-no1vietnam tag-phat tag-top1index tag-top1list tag-top1vietnam tag-trong" id="post-307931"> > .aff-auto-link, .aff-auto-url { color: #0073aa !important; text-decoration: underline dashed #0073aa !important; font-weight: 500; cursor: pointer; transition: all 0.2s; } .aff-auto-link:hover, .aff-auto-url:hover { color: #d32f2f !important; text-decoration: underline solid #d32f2f !important; } .aff-trending-box { margin: 25px 0; padding: 15px; border: 1px dashed #ddd; border-left: 4px solid #d32f2f; background: #f9f9f9; border-radius: 4px; } .aff-trending-box h4 { margin-top: 0; font-size: 16px; color: #333; } .aff-trending-box ul { list-style: none !important; padding: 0 !important; margin: 10px 0 0 !important; display: flex; flex-wrap: wrap; gap: 10px; } .aff-trending-box li a { background: #fff; border: 1px solid #ccc; padding: 5px 12px; border-radius: 20px; font-size: 13px; color: #555 !important; text-decoration: none !important; display: inline-block; transition: 0.3s; } .aff-trending-box li a:hover { background: #d32f2f; color: #fff !important; border-color: #d32f2f; }

Mảng 2 chiều khá quen thuộc với chúng ta. Mình sẽ giới thiệu một vài cách cấp phát động mảng 2 chiều để các bạn lựa chọn cho phù hợp. Ở đây mình minh họa bằng C++, đối với các ngôn ngữ khác thì ý tưởng cũng tương tự vậy thôi. Lưu ý là C++ sẽ không tự động thu hồi tài nguyên động đã cấp phát cho dù có thoát khỏi chương trình, vì thế bắt buộc phải có thao tác giải phóng mảng 2 chiều.

Ý tưởng: Để cấp phát động cho mảng 2 chiều, ta cấp phát bộ nhớ của từng chiều theo cú pháp của mảng một chiều. Tức là tạo m mảng 1 chiều, mỗi mảng có n phần tử.

Để làm điều này ta dùng 1 con trỏ cấp 2, cấp phát cho nó m con trỏ cấp 1, mỗi con trỏ cấp 1 ta lại cấp phát n phần tử.

Ví dụ:

int **a = new int*[m];
for(int i = 0; i<m; i++)
   a[i] = new int[n];

Với ví dụ trên, ta được một mảng động hai chiều các số nguyên có kích thước m x n.

Lưu ý là ta dùng một con trỏ cấp 2 cấp phát cho một mảng các phần tử có kiểu int* chứ không phải kiểu int thông thường. Kiểu int* chính là con trỏ cấp 1.

Để giải phóng bộ nhớ động, ta cũng phải giải phóng theo từng cột và từng hàng.

Ví dụ:

for(int i = 0; i<m; i++)
   delete []a[i];
delete []a;

Cách này phức tạp nhưng bạn sẽ có 1 mảng 2 chiều đúng nghĩa và có thể lấy phần tử bằng cách gọi a[i][j] thông thường như mảng tĩnh.

Ý tưởng: Cấp phát mảng 1 chiều có kích thước m x n và truy cập nó như một mảng 2 chiều với các chỉ số thông qua công thức liên hệ.

Phần tử a[i][j] tương ứng với phần tử a[i*n + j] trong mảng.

Ví dụ:

int *a = new int[m*n]; //cấp phát xong
//khởi tạo bằng 0
for(int i = 0; i < m; i++)
   for(int j = 0; j < n; j++)
      a[i*n+j] = 0;

Việc giải phóng vô cùng đơn giản, chỉ cần 1 lệnh delete.

delete []a;

Với cách này bạn sẽ phải truy cập mảng 2 chiều thông qua công thức liên hệ, tuy nhiên, cách này sẽ hữu ích với những trường hợp như cộng 2 ma trận cùng loại, v.v…

#Cấp #phát #động #cho #mảng #hai #chiều #trong

[bsa_pro_ad_space id=2]

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

 ⭐ ☀ ⚡ 
Born to keep your brand's great stories forever!Bring your brand to the World !

Zalo Viber Telegram WhatsApp Call
top1brand
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart