I want to set a default nonzero value for all elements of a table or 2d array. array[size]={12} sets first elements only 12 and others are all 0 in a row.But fill(array,array+size,12) sets all elements to 12 in a row only.I could't apply this for 2d array.Is there any way to do this using fill() or any way without direct initialization using double for loop
我想为表或2d数组的所有元素设置默认非零值。 array [size] = {12}设置第一个元素只有12个,其他的都是0连续。但是fill(array,array + size,12)将所有元素设置为仅连续12个。我不能将它应用于2d array.Is有任何方法使用fill()或任何方式使用double for循环直接初始化
#include <iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
using namespace std;
int main()
{
int arra[10][10];//declare 2d array
for(int k=0;k<10;k++)//takes k's value 10 for 10 rows
fill(arra,arra+10,45);//select a row and set all columns to 45 didn't work
}
array initialization http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html
数组初始化http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html
4 个解决方案
#1
2
Almost the same way as you would with a 1-dimensional array:
几乎与使用1维数组的方式相同:
#include <iostream>
#include <iomanip>
int main() {
int arr[10][10] = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 },
{ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 },
{ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 },
{ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 },
{ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 },
{ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70 },
{ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 },
{ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 },
{ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }
};
for(int i=0; i<10; ++i) {
for(int j=0; j<10; ++j) {
std::cout << std::setw(4) << arr[i][j];
}
std::cout << '\n';
}
}
NOTE: The first index does not require a value. In that case the compiler will automatically calculate this index.
注意:第一个索引不需要值。在这种情况下,编译器将自动计算此索引。
int arr[][10] = { ... };
EDIT
An alternative that will avoid a double loop is:
避免双循环的替代方案是:
## Heading ###include <iostream>
#include <iomanip>
int main() {
int arr[10][10] = {}; // Initializes all values to 0
for(int i=0; i<10; ++i ) arr[i][0] = 12;
for(int i=0; i<10; ++i) {
for(int j=0; j<10; ++j) {
std::cout << std::setw(4) << arr[i][j];
}
std::cout << '\n';
}
}
#2
1
For C arrays, you'll probably want to use memset
. You've marked this as C++, though, so I feel obliged to give a C++ answer:
对于C数组,您可能希望使用memset。你已经将它标记为C ++,所以我觉得有必要给出一个C ++答案:
std::vector<std::vector<int>> v(10, std::vector<int>(10, 45));
This creates a std::vector
of 10 std::vector<int>
s of size 10 with each element initialized to 45.
这将创建一个大小为10的std :: vector 10 std :: vector
See here for the ideone.
请看这里的想法。
#3
1
A solution (not really elegant, I know) can be
一个解决方案(不是很优雅,我知道)可以
for ( auto & row : arra )
for ( auto & elem : row )
elem = 45;
or, using std::fill()
或者,使用std :: fill()
for ( auto & row : arra )
std::fill(std::begin(row), std::end(row), 45);
---- EDIT ----
----编辑----
Full example
#include <iostream>
int main ()
{
int a[10][10];
// mode A
for ( auto & row : a )
for ( auto & elem : row )
elem = 45;
// mode B
for ( auto & row : a )
std::fill(std::begin(row), std::end(row), 47);
for ( int i = 0 ; i < 10 ; ++i )
{
for ( int j = 0 ; j < 10 ; ++j )
std::cout << '[' << a[i][j] << ']';
std::cout << '\n';
}
return 0;
}
#4
1
This is not much better than memset, but at least you can specify the value for each int instead of each byte with std::fill used like that:
这并不比memset好多少,但至少你可以使用std :: fill指定每个int的值而不是每个字节的值:
#include <algorithm>
#include <stdio.h>
int main() {
int arra[10][10];
std::fill((int*)arra,(int*)arra+sizeof(arra)/sizeof(int),45);
for (auto& row : arra) {
for (auto& x : row)
printf("%d ", x);
puts("");
}
return 0;
}
This relies on the array elements being contiguous in memory.
这依赖于在内存中连续的数组元素。
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
#1
2
Almost the same way as you would with a 1-dimensional array:
几乎与使用1维数组的方式相同:
#include <iostream>
#include <iomanip>
int main() {
int arr[10][10] = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 },
{ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 },
{ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 },
{ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 },
{ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 },
{ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70 },
{ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 },
{ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 },
{ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }
};
for(int i=0; i<10; ++i) {
for(int j=0; j<10; ++j) {
std::cout << std::setw(4) << arr[i][j];
}
std::cout << '\n';
}
}
NOTE: The first index does not require a value. In that case the compiler will automatically calculate this index.
注意:第一个索引不需要值。在这种情况下,编译器将自动计算此索引。
int arr[][10] = { ... };
EDIT
An alternative that will avoid a double loop is:
避免双循环的替代方案是:
## Heading ###include <iostream>
#include <iomanip>
int main() {
int arr[10][10] = {}; // Initializes all values to 0
for(int i=0; i<10; ++i ) arr[i][0] = 12;
for(int i=0; i<10; ++i) {
for(int j=0; j<10; ++j) {
std::cout << std::setw(4) << arr[i][j];
}
std::cout << '\n';
}
}
#2
1
For C arrays, you'll probably want to use memset
. You've marked this as C++, though, so I feel obliged to give a C++ answer:
对于C数组,您可能希望使用memset。你已经将它标记为C ++,所以我觉得有必要给出一个C ++答案:
std::vector<std::vector<int>> v(10, std::vector<int>(10, 45));
This creates a std::vector
of 10 std::vector<int>
s of size 10 with each element initialized to 45.
这将创建一个大小为10的std :: vector 10 std :: vector
See here for the ideone.
请看这里的想法。
#3
1
A solution (not really elegant, I know) can be
一个解决方案(不是很优雅,我知道)可以
for ( auto & row : arra )
for ( auto & elem : row )
elem = 45;
or, using std::fill()
或者,使用std :: fill()
for ( auto & row : arra )
std::fill(std::begin(row), std::end(row), 45);
---- EDIT ----
----编辑----
Full example
#include <iostream>
int main ()
{
int a[10][10];
// mode A
for ( auto & row : a )
for ( auto & elem : row )
elem = 45;
// mode B
for ( auto & row : a )
std::fill(std::begin(row), std::end(row), 47);
for ( int i = 0 ; i < 10 ; ++i )
{
for ( int j = 0 ; j < 10 ; ++j )
std::cout << '[' << a[i][j] << ']';
std::cout << '\n';
}
return 0;
}
#4
1
This is not much better than memset, but at least you can specify the value for each int instead of each byte with std::fill used like that:
这并不比memset好多少,但至少你可以使用std :: fill指定每个int的值而不是每个字节的值:
#include <algorithm>
#include <stdio.h>
int main() {
int arra[10][10];
std::fill((int*)arra,(int*)arra+sizeof(arra)/sizeof(int),45);
for (auto& row : arra) {
for (auto& x : row)
printf("%d ", x);
puts("");
}
return 0;
}
This relies on the array elements being contiguous in memory.
这依赖于在内存中连续的数组元素。
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45