Ú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!
- Kerékpárosok, bringások ide!
- Google Nexus 7 (2013) - ránc: felvarrva
- Meggyi001: Áram nélkül....méltóság nélkül.....
- Fejhallgató erősítő és DAC topik
- Napelem
- Horgász topik
- OLED monitor topic
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- Spórolós topik
- Bambu Lab 3D nyomtatók
- További aktív témák...
- Ausom DT2 Pro elektromos roller
- Lenovo M720s Core i5 9500 // 16GB // 256GB SSD számla + gari - több db elérhető
- 0perces SAMSUNG DDR5 6400MHz vadiúj 2x16GB memória 1 év garancia (6400B)
- AMD Phenom II X4 B95 processzor
- 15.gen! Intel Core ULTRA 9 285K (24mag!) +32GB DDR5 kit +ASROCK Z890 PRO-A! GAR/SZÁMLA (Te nevedre)!
- ÁRGARANCIA! Épített KomPhone Ryzen 7 9800X3D 32/64GB RAM RTX 5090 32GB GAMER PC termékbeszámítással
- GYÖNYÖRŰ iPhone 15 Pro 128GB Blue Titanium -1 ÉV GARANCIA - Kártyafüggetlen, MS4680
- Western Digital SN850 2TB NVMe With Heatsink for PS5
- Hibás Dell Precision 7730 - 17,3", i7 8750H, 32GB RAM, Quadro P3200 6GB VGA
- ASUS ROG Strix SCAR 16 / Ultra 9 275HX / RTX5090 / 32GB / 2TB NVMe! BeszámítOK
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
