Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to use tuple object that hold a collection of elements of a different type in C++

3 Answers

0 votes
#include <iostream>     
#include <tuple>        
     

int main()
{
	std::tuple<int, char> tpl(51, 'a');
	auto test = std::make_tuple("abc", 3.14, 987, 'b');

	std::get<2>(test) = 888;                                    

	int i; char ch;

	std::tie(i, ch) = tpl;
	std::cout << "1. ch = " << ch << std::endl;
	std::cout << "2. i = " << i << std::endl;

	std::tie(std::ignore, std::ignore, i, ch) = test;  
	std::cout << "3. ch = " << ch << std::endl;
	std::cout << "4. i = " << i << std::endl;

	ch = std::get<3>(test);
	std::cout << "5. ch = " << ch << std::endl;
	std::cout << "6. i = " << i << std::endl;

	std::get<0>(tpl) = std::get<2>(test);
	std::cout << "7. get<0>(tpl) = " << std::get<0>(tpl) << std::endl;

	std::get<1>(tpl) = ch;
	std::cout << "8. get<1>(tpl) = " << std::get<1>(tpl) << std::endl;

	return 0;
}


/*
run:

1. ch = a
2. i = 51
3. ch = b
4. i = 888
5. ch = b
6. i = 888
7. get<0>(tpl) = 888
8. get<1>(tpl) = b

*/

 



answered Nov 25, 2017 by avibootz
0 votes
#include <tuple>
#include <iostream>
#include <string>

std::tuple<double, char, std::string> get_info(int idx)
{
	if (idx == 0) return std::make_tuple(3.14, 'X', "C++");
	if (idx == 1) return std::make_tuple(2.89, 'Y', "Python");
	if (idx == 2) return std::make_tuple(2.75, 'Z', "C#");
	throw std::invalid_argument("invalid idx");
}

int main()
{
	auto info = get_info(0);
	std::cout << "INDEX: 0, "
		<< "double: " << std::get<0>(info) << ", "
		<< "char: " << std::get<1>(info) << ", "
		<< "string: " << std::get<2>(info) << std::endl;

	double d;
	char ch;
	std::string s;
	std::tie(d, ch, s) = get_info(1);
	std::cout << "INDEX: 0, "
		<< "double: " << d << ", "
		<< "char: " << ch << ", "
		<< "string: " << s << std::endl;

	return 0;
}


/*
run:

INDEX: 0, double: 3.14, char: X, string: C++
INDEX: 0, double: 2.89, char: Y, string: Python

*/

 



answered Nov 25, 2017 by avibootz
0 votes
#include <tuple>
#include <iostream>
#include <complex>
#include <string>

int main()
{
	std::tuple<std::string, int, int, std::complex<double>> t;

	std::tuple<int, float, std::string> t1(568, 3.14, "c++");

	std::cout << std::get<0>(t1) << " ";
	std::cout << std::get<1>(t1) << " ";
	std::cout << std::get<2>(t1) << " ";
	std::cout << std::endl;

	auto t2 = std::make_tuple(247, 1.17, "python");

	std::get<1>(t1) = std::get<1>(t2);

	std::cout << std::get<0>(t1) << " ";
	std::cout << std::get<1>(t1) << " ";
	std::cout << std::get<2>(t1) << " ";
	std::cout << std::endl;

	return 0;
}


/*
run:

568 3.14 c++
568 1.17 c++

*/

 



answered Nov 25, 2017 by avibootz

Related questions

...