This question already has answers here:
C++ std::get<variable> fails
(4 answers)
Why std::get does not work with variables?
(3 answers)
Cpp Tuple use get with a variable
(1 answer)
Closed 4 months ago.
#include <iostream>
#include <string>
#include <tuple>
int main()
{
auto t = std::make_tuple(1, "Foo", 3.14);
// index-based access
std::cout << "(" << std::get<0>(t) << ", " << std::get<1>(t)
<< ", " << std::get<2>(t) << ")\n";
}
Is it possible to use a variable for the index to access a tuple element instead of a constant?
Related
This question already has answers here:
Can you declare multiple reference variables on the same line in C++?
(2 answers)
Declaring multiple object pointers on one line causes compiler error
(5 answers)
Closed 2 years ago.
Given the answers to the question Is the address of a reference to a dereferenced pointer the same as the address of the pointer? and my current understanding of references, I was very confused, when I checked the addresses of vector elements and compared them to the addresses of references to those elements:
#include <iostream>
#include <vector>
int main(){
std::vector<int> vec { 1, 2, 3, 4 };
const int&
ref0 { vec[0] },
ref1 { vec[1] },
ref2 { vec[2] },
ref3 { vec[3] };
std::cout
<< &(vec[0]) << " vs " << &ref0 << "\n"
<< &(vec[1]) << " vs " << &ref1 << "\n"
<< &(vec[2]) << " vs " << &ref2 << "\n"
<< &(vec[3]) << " vs " << &ref3 << "\n";
return 0;
}
Output on my machine (Ubuntu 20.04, compiled with g++ 9.3.0, default options):
0x561553dbdeb0 vs 0x561553dbdeb0
0x561553dbdeb4 vs 0x7fff539f4d6c
0x561553dbdeb8 vs 0x7fff539f4d70
0x561553dbdebc vs 0x7fff539f4d74
So the address of the first element and the address of the reference to the first element are the same, but all others are not. Why is that?
This is a simple typo: the & in the declaration only applies to ref0, the others are non-reference types!
Did you want
const int
&ref0 { vec[0] },
&ref1 { vec[1] },
&ref2 { vec[2] },
&ref3 { vec[3] };
This is more frequently fallen for with pointer declarations like int* p, q;.
This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.
This question already has answers here:
Function call operator [duplicate]
(2 answers)
What are C++ functors and their uses?
(14 answers)
Closed 2 years ago.
I have seen this question in a C++ test and I have no idea what the solution should be.
So in the code below, what is absValue?
#include <iostream>
struct absValue
{
float operator()(float f) {
return f > 0 ? f : -f;
}
};
int main( )
{
using namespace std;
float f = -123.45; absValue aObj;
float abs_f = aObj(f);
cout << "f = " << f << " abs_f = " << abs_f << endl;
return 0;
}
This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?
My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I am expecting to get this output: 00A7
You also need setfill('0').