Ú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!
- Horgász topik
- CASIO órák kedvelők topicja!
- Folyószámla, bankszámla, bankváltás, külföldi kártyahasználat
- Nintendo Switch 2
- Sokkal jobb ajánlat lett elődjénél az iPhone 17e
- Kazy Computers - Fehérvár - Megbízható?
- Mibe tegyem a megtakarításaimat?
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- Hosszú premier előzetest kapott az Arknights: Endfield
- Formula-1
- További aktív témák...
- Samsung 870EVO 1TB 2.5"
- HP Probook 445R G6,14",FHD,AMD Ryzen 5 3500U,8GB DDR4,256GB SSD,WIN11
- Mind a új MAKULÁTLAN Apple MacBook Air 15" M2 16B RAM 256GB Magyar billentyű garancia 2026.11.14
- Lenovo ThinkPad T480s,14",FHD,i5-7300U,8GB DDR4,256GB SSD,WIN11,TOUCH
- GARANCIÁLIS ASUS TUF F16 // Intel Core 5 210H // 16GB RAM // 1TB SSD // RTX 4050
- HP EliteBook 840 G6, G5 14" i5, 8-16GB RAM, SSD, jó akku, számla, 6 hó gar
- MacBook Air 15" (M3, 8 GB RAM, 512 GB SSD)
- HIBÁTLAN iPhone 13 256GB Starlight-1 ÉV GARANCIA - Kártyafüggetlen, MS4676, 100% AKKSI
- iKing.Hu - Használt, karcmentes Google Pixel 9 Pro Fold Obsidian színben, 256 GB tárhellyel, 6 hónap
- 8Gb DDR3L 1,35V 12800u 1600Mhz RAM-ok, több db
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
