Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Filling an array with some initial value
- Filling an static int array
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int staticArray[3000];
const int InitValue = 9990;
int main()
{
cout << "Uninitialized Array:"<<staticArray[3000-1] << endl;
std::fill(staticArray,staticArray+(end(staticArray) - begin(staticArray)),InitValue);
cout << "Array filled with:"<<staticArray[3000-1] << endl;
}
Enter to Rename, Shift+Enter to Preview
- Filling a vector array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
vector<int> dynArray;
const int InitValue = 9990;
int main()
{
dynArray.resize(3000);
cout << "Uninitialized Array:"<<dynArray.back() << endl;
std::fill(dynArray.begin(),dynArray.end(),InitValue);
cout << "Array filled with:"<<dynArray.back() << endl;
}
Enter to Rename, Shift+Enter to Preview
- Filling a vector array
The object for initialization is a complex one. std::fill works exactly the same.
Avoid memset as much as possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
class Pod{
public:
float x,y;
float vx,vy;
Pod(){ } //No initialization
Pod(float _x,float _y, float _vx, float _vy){
x=_x;y=_y;vx=_vx;vy=_vy;
}
};
vector<Pod> dynArray;
const Pod InitValue(0.6f,11.2f,0.45f,0.99f);
int main()
{
dynArray.resize(3000);
cout << "Uninitialized Vector:"<<dynArray.back().x<<","<<dynArray.back().y<< endl;
std::fill(dynArray.begin(),dynArray.end(),InitValue);
cout << "Array filled with:"<<dynArray.back().x<<","<<dynArray.back().y<< endl;
}
Enter to Rename, Shift+Enter to Preview
Check http://en.cppreference.com/w/cpp/algorithm/fill for more info
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content