11 Ekim 2017 Çarşamba

tuple

Tuple Sınıfı
Giriş
Şu satırı dahil ederiz.
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
Şu satırı dahil ederiz.
using namespace boost::tuples;
Açıklaması şöyle
boost::tuple was created almost two decades ago, and std::tuple was introduced to the core Standard Library in C++11, in 2011, only 6 years ago.

They're not "interchangable", for a given definition of the term "interchangable". You can't assign a std::tuple<> to a boost::tuple<> or vice-versa, because even if their implementation is the same, they still represent distinct objects.

However, because they are essentially the same, you can do a find→replace for boost::tuple→std::tuple and more-or-less arrive with identically behaving and performing code, and because dependency on the boost libraries is not something every programmer can have, it's almost universally recommended that any project which has access to >=C++11 prefer std::tuple in all cases.
get metodu
Şöyle yaparız.
typedef boost::tuple<std::string, int, bool> animal;
animal a = boost::make_tuple("cat", 4, true);
a.get<0>() = "dog";
std::cout << std::boolalpha << a << '\n';
operator == metodu
Karşılaştırma için şu satırı dahil ederiz.
#include <boost/tuple/tuple_comparison.hpp>
Açıklaması şöyle
Tuples reduce the operators ==, !=, <, >, <= and >= to the corresponding elementary operators. This means, that if any of these operators is defined between all elements of two tuples, then the same operator is defined between the tuples as well.
Şöyle yaparız.
tuple<int, int> t1 (0, 0);
tuple<int, int> t2 (0, 0);

if (t1 == t2) { cout << "equal\n"; } else { cout << "notequal\n"; }
operator << metodu
Input output için şu satırı dahil ederiz.
#include <boost/tuple/tuple_io.hpp>
Açıklaması şöyle
The global operator<< has been overloaded for std::ostream such that tuples are output by recursively calling operator<< for each element.
Şöyle yaparız.
tuple<int, int> t (0, 0);

cout << "t: " << t << endl;
Constructor 
Şöyle yaparız.
tuple<int, int> t (0, 0);
Şöyle yaparız.
boost::tuple<int*, Foo*, std::string*> t (nullptr, nullptr, nullptr);
Free Style Metodlar
get metodu
Şöyle yaparız.
bool result = boost::get<0>(t) == 1;
make_tuple metodu
Şöyle yaparız.
using tup_t =  boost::tuple<int,std::string, std::string, std::string>;
tup_t t = boost::make_tuple(2, "hello", "world", "bye"); 
Vector olarak yaratmak istersek şöyle yaparız.
std::vector<tup_t> games{
  boost::make_tuple(2, "hello", "world", "bye"), 
  boost::make_tuple(1, "foo", "bar", "baz")
};

Hiç yorum yok:

Yorum Gönder