簡體   English   中英

如何使用 memset 填充數組的最大值?

[英]How to use memset to fill in max value for an array?

這是我的代碼,我需要用最大大小(INT_MAX)填充二維數組中的每個元素,但是,我的代碼無法正常工作?

#include <iostream>
#include <string.h>
#include <limits.h>
using namespace std;
int main(){
    int arr[10][10];
    memset(arr,INT_MAX, sizeof (arr));
    cout << arr[0][3];
//output = -1 instead of 2147483647


}

如何修改我的代碼,以便能夠用最大值填充數組?

C++ 標准庫有一個 function fill_nn值填充 arrays。

#include <algorithm>
...
std::fill_n(&arr[0][0], 100, INT_MAX);

這里值的數量可能很脆弱 - 如果您將來更改數組的大小,您必須記住將硬編碼的100更改為新的正確值。

對於快速破解來說已經足夠了。 如果您需要具有長期價值的東西,請使用此答案中的想法來回答類似問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM