Új hozzászólás Aktív témák
-
Jester01
veterán
Ha megoldható, akkor a tömböt add át, és a függvényen belül legyen a .adatx. Ez gondolom menne, tehát valamiért nem megvalósítható.
Alternatívaként csinálhatsz accessor függvényt, varázsolhatsz offsetof segítségével avagy ha esetleg véletlen c++ és nem c akkor member pointerrel.
#include <iostream>
using namespace std;
struct Foo
{
int x;
int y;
};
int sum0_x(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].x;
}
return r;
}
int sum0_y(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].y;
}
return r;
}
int sum1(Foo* array, int count, int (*accessor)(Foo*))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += accessor(array + i);
}
return r;
}
int sum2(Foo* array, int count, int member)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += *(int*)((char*)(array + i) + member);
}
return r;
}
int sum3(Foo* array, int count, int (Foo::*member))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].*member;
}
return r;
}
int accessor_x(Foo* foo)
{
return foo->x;
}
int accessor_y(Foo* foo)
{
return foo->y;
}
int main()
{
Foo foos[2];
foos[0].x = 1;
foos[0].y = 2;
foos[1].x = 3;
foos[1].y = 4;
cout << "sum0(x) = " << sum0_x(foos, 2) << endl;
cout << "sum0(y) = " << sum0_y(foos, 2) << endl;
cout << "sum1(x) = " << sum1(foos, 2, accessor_x) << endl;
cout << "sum1(y) = " << sum1(foos, 2, accessor_y) << endl;
cout << "sum2(x) = " << sum2(foos, 2, offsetof(Foo, x)) << endl;
cout << "sum2(y) = " << sum2(foos, 2, offsetof(Foo, y)) << endl;
cout << "sum3(x) = " << sum3(foos, 2, &Foo::x) << endl;
cout << "sum3(y) = " << sum3(foos, 2, &Foo::y) << endl;
return 0;
}
Új hozzászólás Aktív témák
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- AI okozta csődhullámra figyelmeztett a Phison vezetője
- Milyen routert?
- World of Tanks - MMO
- Apple iPhone SE (3. generáció) - szélsebes múltidézés
- Lítium-ion/Li-ion akkumulátorok
- Multimédiás / PC-s hangfalszettek (2.0, 2.1, 5.1)
- Hitelkártyák használata, hitelkártya visszatérítés
- Kínai és egyéb olcsó órák topikja
- gban: Ingyen kellene, de tegnapra
- Nem indul és mi a baja a gépemnek topik
- További aktív témák...
- Asus Rog Strix G513R // Ryzen 7 6800H // 16GB RAM // Samsung SSD 990 Pro 2TB + 512GB SSD // RTX 3060
- Asus Rog Strix G713R // Ryzen 9 6900HX // 32GB RAM // 1TB SSD // RTX 3080 8GB
- Prestige 14Evo A12M 14" FHD IPS i5-1240P 16GB 256GB NVMe magyar vbill gar
- Új magyar Thinkpad T14 gen5 Ultra 7 155U 16/32GB 512GB 2028-ig garis! Csak februárban extra akció!!
- Akció! Csere-Beszámítás! Asus Zenbook 14 UM425IA! R7 4700U / 8GB / 512GB SSD!
- REFURBISHED - DELL Universal Dock D6000 (452-BCYH) (DisplayLink)
- Apple iPhone 14 Pro Max 256GB,Újszerű,Dobozaval,12 hónap garanciával
- Wacom Bamboo One CTF-430 rajztábla
- Dell Latitude 9410 i7 / 16GB RAM / 512GB SSD 2in1 érintőkijelző Profi gép, kedvező áron!
- Dell Latitude 9420 i5-1145G7 14" FHD+ 16GB 512GB 1 év garancia
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest

