Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 30, 2024 10:09
Show Gist options
  • Save juanfal/67cf1a014cc6eeb2bd75b26f3d2eb34b to your computer and use it in GitHub Desktop.
Save juanfal/67cf1a014cc6eeb2bd75b26f3d2eb34b to your computer and use it in GitHub Desktop.
array quick init
// 00.quickinit.cpp
// juanfc 2024-11-30
// https://gist.github.com/juanfal/67cf1a014cc6eeb2bd75b26f3d2eb34b
#include <iostream>
#include <array>
using namespace std;
const int N = 3;
const int NN = 100;
typedef array<int,N> TVec;
typedef array<int,NN> TLongVect;
int main()
{
void print(TVec v);
TVec a = {1, 2, 3};
TVec b = {{1, 2, 3}}; // More general!!
print(a);
print(b);
int dummyNot0Val = 33;
TLongVect lv = {dummyNot0Val};
int cnt = 0;
for (int i = 0; i < NN; ++i)
if (lv[i] != dummyNot0Val)
++cnt;
if (cnt > 0) {
printf("I have counted %d (%g%%) different elements of the initialization %d\n",
cnt, (float)100*cnt/NN, dummyNot0Val);
}
return 0;
}
void print(TVec v)
{
for (int i = 0; i < N; ++i)
cout << v[i] << " ";
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment