c++ - Defining a type that holds 3 int value -


i'm new c++ , not figure out how can define variable holds 3 values, e.g. coordinates hold 2 values, (x,y).

i tried:

typedef int u_k(int a,int b,int c); 

but doesn't seem work.

i'd appreciate quick simple answer :)

thanks!

edit: did :

struct u_k{     float a,b,c; };     u_k uk; //this line 

is wrong? because "unknown type name u_k" line... first though because needed declare under function going use struct for, turns out there error both cases.

the shortest way use struct

struct u_k {     int a,b,c; }; 

usage:

u_k tmp; tmp.a = 0; tmp.b = 1; tmp.c = 2; 

you can add complexity type adding member function/constructors make usage of u_k easier:

struct u_k {     int a,b,c;     u_k() //default constructor         :a(0)         ,b(0)         ,c(0)     {}     u_k(int _a_value,int _b_value, int _c_value) //constructor custom values         :a(_a_value)         ,b(_b_value)         ,c(_c_value)     {}  }; //usage: int main() {     u_k tmp(0,1,2);     std::cout << "a = " << tmp.a << std::endl;//print     std::cout << "b = " << tmp.b << std::endl;//print b     std::cout << "c = " << tmp.c << std::endl;//print c } 

alternatively can use std::tuple obtain same result. using different:

std::tuple<int,int,int> t = std::make_tuple(0,1,2); std::cout << "a = " << std::get<0>(t) << std::endl;//print first member std::cout << "b = " << std::get<1>(t) << std::endl;//print second member std::cout << "c = " << std::get<2>(t) << std::endl;//print third member 

if learning c++ now should know implementation std::tuple more complex trivial struct , understand need learn templates , variadic templates.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -