tag:blogger.com,1999:blog-66462854753390954152024-02-20T05:03:09.569-05:00C++ TriviaC++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.comBlogger1005125tag:blogger.com,1999:blog-6646285475339095415.post-57823254385574891112017-12-27T17:30:00.001-05:002017-12-27T17:30:30.868-05:00C++11: Inline Function<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;">A function declared constexpr is
implicitly declared an inline function.</span><span style="font-family: "Courier New"; font-size: 8.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;">Reference: <a href="http://en.cppreference.com/w/cpp/language/inline">http://en.cppreference.com/w/cpp/language/inline</a><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com2tag:blogger.com,1999:blog-6646285475339095415.post-31601163883354389462017-12-27T17:29:00.003-05:002017-12-27T17:29:50.744-05:00C++11: std::vector::shrink_to_fit()C++11 added the vector function shrink_to_fit() to suggest to the system to reduce the capacity to size. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <vector>
int main()
{
std::vector<int> myVector;
std::cout << myVector.capacity() << " "; // 15
myVector.resize(1000);
std::cout << myVector.capacity() << " "; // 1007
myVector.clear();
myVector.shrink_to_fit();
std::cout << myVector.capacity() << " "; // 15
std::cout << std::endl;
return 0;
}
// Output: 0 1000 0
</int></span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit">http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-23995170197927197352017-12-27T17:28:00.003-05:002017-12-27T17:28:41.227-05:00C++11: std::basic_string::shrink_to_fit()C++11 added the string function shrink_to_fit() to suggest to the system to reduce the capacity to size. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <string>
int main()
{
std::string myString;
std::cout << myString.capacity() << " "; // 15
myString.resize(1000);
std::cout << myString.capacity() << " "; // 1007
myString.clear();
myString.shrink_to_fit();
std::cout << myString.capacity() << " "; // 15
std::cout << std::endl;
return 0;
}
// Output: 15 1007 15
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/string/basic_string/shrink_to_fit">http://en.cppreference.com/w/cpp/string/basic_string/shrink_to_fit</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-16112633656818262152017-12-27T17:27:00.000-05:002017-12-27T17:27:15.340-05:00C++98: std::istream_iteratorThe istream_iterator template class wraps a stream and returns an input iterator. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <iterator>
int main()
{
int myInt;
std::istream_iterator<int> myIstreamIterator(std::cin);
myInt = *myIstreamIterator;
std::cout << myInt << " "; // 1
myIstreamIterator++;
myInt = *myIstreamIterator;
std::cout << myInt << " "; // 2
myIstreamIterator++;
myInt = *myIstreamIterator;
std::cout << myInt << " "; // 3
std::cout << std::endl;
return 0;
}
// Input : 1 2 3
// Output: 1 2 3
</span></pre>
Reference: <a href="http://www.cplusplus.com/reference/iterator/istream_iterator/">http://www.cplusplus.com/reference/iterator/istream_iterator/</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-51757350214328757642017-12-27T17:23:00.002-05:002017-12-27T17:23:37.050-05:00C++98: The boolalpha and nobool alpha Manipulators on cinThe boolalpha manipulator changes the way Boolean values are input from cin. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
int main()
{
bool dTrue = false;
bool dFalse = false;
bool szTrue = false;
bool szFalse = false;
std::cin >> std::boolalpha;
std::cin >> dTrue;
std::cin >> dFalse;
std::cin >> szTrue;
std::cin >> szFalse;
std::cout << dTrue << " "; // 1
std::cout << dFalse << " "; // 0
std::cout << szTrue << " "; // 1
std::cout << szFalse << " "; // 0
std::cin >> std::noboolalpha;
std::cin >> dTrue;
std::cin >> dFalse;
std::cin >> szTrue;
std::cin >> szFalse;
std::cout << dTrue << " "; // 1
std::cout << dFalse << " "; // 0
std::cout << szTrue << " "; // 1
std::cout << szFalse << " "; // 0
std::cout << std::endl;
return 0;
}
// Input : true false true false 1 0 1 0
// Output: 1 0 1 0 1 0 1 0
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 617.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-61310212449994321562017-12-27T17:22:00.001-05:002017-12-27T17:22:22.028-05:00C++98: The boolalpha and noboolalpha Manipulators on coutThe boolalpha manipulator changes the way Boolean values are output to cout. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
int main()
{
std::cout << true << " "; // 1
std::cout << false << " "; // 0
std::cout << std::boolalpha;
std::cout << true << " "; // true
std::cout << false << " "; // false
std::cout << std::noboolalpha;
std::cout << true << " "; // 1
std::cout << false << " "; // 0
std::cout << std::endl;
return 0;
}
// Output: 1 0 true false 1 0
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 617.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-40438196382434311672017-12-27T17:18:00.004-05:002017-12-27T17:18:55.365-05:00C++98: Standard StreamsThere are eight C++ standard streams. The four narrow character streams are: cin, cout, cerr, and clog. The four wide-character streams are wcin, wcout, wcerr, and wclog.<br />
<br />
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 682.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-54549558645816490212017-12-27T17:17:00.001-05:002017-12-27T17:17:40.743-05:00C++98: User-Defined ManipulatorsYou can write your own stream manipulators. Here is an example:
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <istream>
template <class charT, class traits>
inline std::basic_istream<charT, traits>&
printNextChar(std::basic_istream<charT, traits>& stream)
{
char nextChar;
nextChar = std::cin.get();
std::cin.unget();
std::cout << nextChar;
return stream;
}
int main()
{
char myChar;
std::cin >> printNextChar;
std::cin >> myChar;
std::cout << myChar << std::endl;
return 0;
}
// Input: A
// Output: AA
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 614.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-8647100089033702712017-12-27T17:16:00.000-05:002017-12-27T17:16:05.402-05:00C++98: cin.ignore()The cin.ignore() function allows for a sequence of input characters to be ignored. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
int main()
{
int myInt1 = 0;
int myInt2 = 0;
std::cin >> myInt1; // 1
std::cin.ignore(256, '\n'); // Extracts and discards characters
std::cin >> myInt2; // 3
std::cout << myInt1 << " "; // 1
std::cout << myInt2 << " "; // 3
std::cout << std::endl;
return 0;
}
// Input:
// 1 2
// 3 4
// Output:
// 1 3
</span></pre>
Reference: <a href="http://www.cplusplus.com/reference/istream/istream/ignore/">http://www.cplusplus.com/reference/istream/istream/ignore/</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-47380109169411978472017-12-27T17:14:00.000-05:002017-12-27T17:14:07.358-05:00C++98, 11, 14: Complex NumbersThe std::complex template class handles complex numbers. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
// Euler's Identity: e**(i * pi) + 1 = 0
#include <complex>
#include <iostream>
const std::complex<double> I(0.0, 1.0);
const double PI = 3.141592653589793238463;
int main()
{
std::complex<double> eRaisedToTheITimesPiPower;
std::complex<double> lhs ;
std::complex<double> rhs ;
double lhsAbs ;
double rhsAbs ;
double lhsArg ;
double rhsArg ;
double lhsReal;
double rhsReal;
double lhsImag;
double rhsImag;
std::complex<double> lhs2 ;
std::complex<double> rhs2 ;
bool lhsIsEqualToRhs ;
bool lhs2IsEqualToRhs2;
eRaisedToTheITimesPiPower = std::exp(I * PI);
lhs = eRaisedToTheITimesPiPower + 1.0;
rhs = std::complex<double>(0.0, 0.0);
lhsAbs = std::abs (lhs);
rhsAbs = std::abs (rhs);
lhsArg = std::arg (lhs);
rhsArg = std::arg (rhs);
lhsReal = std::real(lhs);
rhsReal = std::real(rhs);
lhsImag = std::imag(lhs);
rhsImag = std::imag(rhs);
lhs2 = std::polar<double>(lhsReal, lhsImag);
rhs2 = std::polar<double>(rhsReal, rhsImag);
lhsIsEqualToRhs = (lhs == rhs );
lhs2IsEqualToRhs2 = (lhs2 == rhs2);
std::cout << eRaisedToTheITimesPiPower << " ";
std::cout << lhs << " ";
std::cout << rhs << " ";
std::cout << lhsAbs << " ";
std::cout << rhsAbs << " ";
std::cout << std::endl;
std::cout << lhsArg << " ";
std::cout << rhsArg << " ";
std::cout << lhsReal << " ";
std::cout << rhsReal << " ";
std::cout << lhsImag << " ";
std::cout << rhsImag << " ";
std::cout << lhs2 << " ";
std::cout << rhs2 << " ";
std::cout << lhsIsEqualToRhs << " ";
std::cout << lhs2IsEqualToRhs2 << " ";
std::cout << std::endl;
return 0;
}
// Output: (-1,1.22465e-16) (0,1.22465e-16) (0,0) 1.22465e-16 0
// 1.5708 0 0 0 1.22465e-16 0 (0,0) (0,0) 0 1
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/numeric/complex">http://en.cppreference.com/w/cpp/numeric/complex</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-15204468561938283182017-12-19T13:50:00.004-05:002017-12-19T13:50:35.792-05:00C++98: std::reverse()std::reverse() reverses elements in a range. Strings have their own reverse() function. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
#include <iterator>
int main()
{
// Index: 0 1 2 3 4 5 6 7
int sequence[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int * iter_beg = &sequence[0];
int * iter_end = &sequence[8];
std::reverse(iter_beg, iter_end);
std::copy(&sequence[0] ,
&sequence[8] ,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
// Output: 8 7 6 5 4 3 2 1
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 386-7.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-2896948004660452182017-12-19T13:49:00.000-05:002017-12-19T13:52:08.259-05:00C++98: std::swap_ranges()std::swap_ranges() swaps elements of two ranges. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
#include <iterator>
int main()
{
// Index: 0 1 2 3 4 5 6 7
int sequence1[8] = {1, 3, 3, 3, 1, 1, 1, 1};
int sequence2[8] = {1, 1, 1, 1, 4, 4, 4, 1};
int * iter_beg1 = &sequence1[1] ; // Point to first 3.
int * iter_end1 = &sequence1[4] ; // Point after last 3.
int * iter_beg2 = &sequence2[4] ; // Point to first 4.
int * iter_end2 = NULL ;
iter_end2 = std::swap_ranges(iter_beg1, // Points after original first 4.
iter_end1,
iter_beg2);
std::copy(&sequence1[0] ,
&sequence1[8] ,
std::ostream_iterator<int>(std::cout, " "));
std::cout << ": ";
std::copy(&sequence2[0] ,
&sequence2[8] ,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
// Output: 1 4 4 4 1 1 1 1 : 1 1 1 1 3 3 3 1
</int></int></span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 370-1.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-60983921483544586172017-12-19T13:47:00.002-05:002017-12-19T13:47:51.334-05:00C++98: std::lexicographical_compare()std::lexicographical_compare() tests for ‘lexigraphically less than’ on two ranges. For strings, it is functionally equivalent to ‘<’. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
int main()
{
int sequence1[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence2[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence3[8] = {1, 3, 2, 4, 5, 7, 6};
bool lhsIsLessThanRhs = false;
lhsIsLessThanRhs = std::lexicographical_compare(&sequence1[0],
&sequence1[8],
&sequence2[0],
&sequence2[8]);
std::cout << lhsIsLessThanRhs << " "; // 0
lhsIsLessThanRhs = std::lexicographical_compare(&sequence2[0],
&sequence2[8],
&sequence3[0],
&sequence3[8]);
std::cout << lhsIsLessThanRhs << " "; // 0
lhsIsLessThanRhs = std::lexicographical_compare(&sequence3[0],
&sequence3[8],
&sequence1[0],
&sequence1[8]);
std::cout << lhsIsLessThanRhs << " "; // 1
std::cout << std::endl;
return 0;
}
// Output: 0 0 1
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 360-2.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-68379153404006476772017-12-19T13:45:00.000-05:002017-12-19T13:45:25.016-05:00C++98: std::mismatch()The template function std::mismatch() returns the pair of iterators where two sequences mismatch. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
int main()
{
int sequence1[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence2[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence3[8] = {1, 3, 2, 4, 5, 7, 6, 1};
std::pair<int int=""> mismatchIterators;
mismatchIterators = std::mismatch(&sequence1[0],
&sequence1[8],
&sequence2[0]);
std::cout << mismatchIterators.first -&sequence1[0] << " "; // 8 (Index)
std::cout << mismatchIterators.second-&sequence2[0] << " "; // 8 (Index)
mismatchIterators = std::mismatch(&sequence1[0],
&sequence1[8],
&sequence3[0]);
std::cout << *mismatchIterators.first << " "; // 9 (Value)
std::cout << *mismatchIterators.second << " "; // 1 (Value)
std::cout << std::endl;
return 0;
}
// Output: 8 8 9 1
</int></span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 158-60.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-13552324209390823392017-12-19T13:43:00.001-05:002017-12-19T13:43:31.163-05:00C++98: std::max_size()You can query what the maxmum size of container is by using the template function std::max_size(). Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <vector>
int main()
{
std::vector<char > Chars ;
std::vector<short > Shorts;
std::vector<int > Ints ;
std::vector<long long> Longs ;
std::cout << Chars .max_size() << " ";
std::cout << Shorts.max_size() << " ";
std::cout << Ints .max_size() << " ";
std::cout << Longs .max_size() << " ";
std::cout << std::endl;
return 0;
}
// Output: 2147483647 2147483647 1073741823 536870911
// Notes:
// 2147483647 (= 2.0 G - 1 = 31 bits)
// 1073741823 (= 1.0 G - 1 = 30 bits)
// 536870911 (= 0.5 G - 1 = 29 bits)
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 145.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-88471214393833441572017-12-19T13:41:00.001-05:002017-12-19T13:41:13.747-05:00C++98: std::Equal()The template function std::Equal() compares two sequences. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
int main()
{
int sequence1[8] = {1, 3, 2, 4, 5, 7, 6, 8};
int sequence2[8] = {1, 3, 2, 4, 5, 7, 6, 8};
int sequence3[8] = {1, 3, 2, 4, 5, 7, 6, 1};
std::cout << std::equal(&sequence1[0], &sequence1[8], &sequence2[0]) << " ";
std::cout << std::equal(&sequence1[0], &sequence1[8], &sequence3[0]) << " ";
std::cout << std::endl;
return 0;
}
// Output: 1 0
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 101.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-74379466493004560612017-12-19T13:39:00.000-05:002017-12-20T13:43:46.633-05:00C++98: Priority QueuesPriority Queues let items go to the head of the queue based on priority. Items with the same priority are not necessarily taken in order. Here is an example:
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <iostream>
#include <queue>
enum Priority
{
LOW,
MEDIUM,
HIGH
};
class Message
{
public:
Priority mPriority;
int mMessage;
Message(int message)
: mMessage(message), mPriority(MEDIUM)
{}
Message(int message, Priority priority)
: mMessage(message), mPriority(priority)
{}
};
struct PriorityLessThan
{
bool operator()(Message lhs, Message rhs)
{
if ((int)lhs.mPriority < (int)rhs.mPriority)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
std::priority_queue<Message ,
std::vector<Message>,
PriorityLessThan > priorityQueue;
priorityQueue.push(Message(1 )); // 4th or 5th
priorityQueue.push(Message(2, HIGH )); // 1st or 2nd or 3rd
priorityQueue.push(Message(3, LOW )); // 6th or 7th
priorityQueue.push(Message(4, HIGH )); // 1st or 2nd or 3rd
priorityQueue.push(Message(5, LOW )); // 6th or 7th
priorityQueue.push(Message(6, MEDIUM)); // 4th or 5th
priorityQueue.push(Message(7, HIGH )); // 1st or 2nd or 3rd
for (int i = 0; i < 7; i++)
{
std::cout << priorityQueue.top().mMessage << " ";
priorityQueue.pop();
}
std::cout << std::endl;
return 0;
}
// Output: 2 7 4 6 1 3 5
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 453-9.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-54354315269875129502017-12-19T13:34:00.005-05:002017-12-19T13:34:57.000-05:00C++98: std::inner_product()The std::inner_product() template function computes the inner product of two sequences. Here is an example:
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
// Alternatively,
// Properties > Configuration Properties > C/C++ > Code Generation >
// Security Check > Disable Security Check(/GS-)
#include <iostream>
#include <numeric>
int main()
{
int vector1[3] = {1, 2, 3};
int vector2[3] = {4, 5, 6};
int innerProduct = 0;
innerProduct = std::inner_product(&vector1[0],&vector1[3],&vector2[0],0);
std::cout << innerProduct << std::endl; // 32 = 1*4 + 2*5 + 3*6
return 0;
}
// Output: 32
</span></pre>
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 427-8.
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-26820151411243019962017-12-19T13:33:00.002-05:002017-12-19T13:33:32.679-05:00C++11: std::fpclassify()C++11 added the function std::classify(). It classifies a floating point number. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <cfloat>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << fpclassify( 0.0 ) << " "; // 0 (=FP_ZERO )
cout << fpclassify(-0.0 ) << " "; // 0 (=FP_ZERO )
cout << fpclassify(-42.0 ) << " "; // -1 (=FP_NORMAL )
cout << fpclassify(DBL_MIN/2.0) << " "; // -2 (=FP_SUBNORMAL)
cout << fpclassify(NAN ) << " "; // 2 (=FP_NAN )
cout << fpclassify(INFINITY ) << " "; // 1 (=FP_INFINITE )
cout << ": ";
cout << (0.0 == -0.0 ) << " "; // 1
cout << (DBL_MIN/2.0 == DBL_MIN ) << " "; // 0
cout << (DBL_MIN/2.0 == DBL_MIN/4.0 ) << " "; // 0
cout << (DBL_MIN/DBL_MAX/2.0 == DBL_MIN/DBL_MAX/4.0) << " "; // 1
cout << endl;
return 0;
}
// Output: 0 0 -1 -2 2 1 : 1 0 0 1
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/numeric/math/fpclassify">http://en.cppreference.com/w/cpp/numeric/math/fpclassify</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-62707546833971267472017-12-19T13:31:00.000-05:002017-12-19T13:31:12.591-05:00C++11: std::cbrt()C++11 added the std::cbrt() function. It computes the cube root. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << cbrt( 1.0) << " "; // 1
cout << cbrt( 8.0) << " "; // 2
cout << cbrt( 27.0) << " "; // 3
cout << cbrt( 64.0) << " "; // 4
cout << cbrt(125.0) << " "; // 5
cout << cbrt(216.0) << " "; // 6
cout << cbrt( 2.0) << " "; // 1.25992
cout << cbrt( 3.0) << " "; // 1.44225
cout << endl;
return 0;
}
// Output: 1 2 3 4 5 6 1.25992 1.44225
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/numeric/math/cbrt">http://en.cppreference.com/w/cpp/numeric/math/cbrt</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-8555589069829445052017-12-11T17:20:00.000-05:002017-12-11T17:20:09.414-05:00C++14: The Rule of Three/Five/Zero<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><b>Rule of Three</b> – If a class requires
either user-defined destructor, copy
constructor, or copy assignment, it usually requires all three.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><b>Rule of Five</b> – If Rule of Three is in
action, and also move semantics are needed then the move constructor and move
assignment operator are also needed.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><b>Rule of Zero</b> – “Classes that have custom
destructors, copy/move constructors or copy/move assignment operators should
deal exclusively with ownership.” – R. Martinho Fernandes<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;">References:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><a href="http://en.cppreference.com/w/cpp/language/rule_of_three">http://en.cppreference.com/w/cpp/language/rule_of_three</a><o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-size: 10.0pt;"><a href="https://blog.rmf.io/cxx11/rule-of-zero">https://blog.rmf.io/cxx11/rule-of-zero</a><o:p></o:p></span></div>
C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-65116103282741100912017-12-11T17:18:00.002-05:002017-12-11T17:18:38.939-05:00C++14: std::exchange()C++14 added the template function std::exchange(). Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <utility>
#include <iostream>
using namespace std;
int main()
{
int myObject = 1;
int myOldValue = 0.0;
myOldValue = exchange(myObject, 9);
cout << myOldValue << " " << myObject;
cout << endl;
return 0;
}
// Output: 1 9
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/utility/exchange">http://en.cppreference.com/w/cpp/utility/exchange</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-31073065920064967162017-12-11T17:17:00.000-05:002017-12-11T17:17:10.645-05:00C++11: std::rotate()C++11 added the std::rotate function. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
// 0 1 2 3 4
// - - - - -
int myArray[] = {1, 2, 3, 4, 5};
int * myArrayBeginIterator = &myArray[0];
int * myArrayMakeFirstIterator = &myArray[3];
int * myArrayEndIterator = &myArray[5];
rotate(myArrayBeginIterator ,
myArrayMakeFirstIterator,
myArrayEndIterator );
for (int * iter = myArrayBeginIterator;
iter != myArrayEndIterator ;
++iter )
{
cout << *iter << " ";
}
cout << endl;
return 0;
}
// Output: 4 5 1 2 3
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/algorithm/rotate">http://en.cppreference.com/w/cpp/algorithm/rotate</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-75983084676141005212017-12-11T17:12:00.002-05:002017-12-11T17:12:28.012-05:00C++11: std::nearbyint()C++11 added the std::nearbyint() function. Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << nearbyint(0.0) << " "; // 0
cout << nearbyint(0.5) << " "; // 0
cout << nearbyint(1.5) << " "; // 2
cout << nearbyint(2.0) << " "; // 2
cout << nearbyint(2.5) << " "; // 2
cout << ": ";
cout << nearbyint(-0.0) << " "; // -0
cout << nearbyint(-0.5) << " "; // -0
cout << nearbyint(-1.5) << " "; // -2
cout << nearbyint(-2.0) << " "; // -2
cout << nearbyint(-2.5) << " "; // -2
cout << endl;
return 0;
}
// Output: 0 0 2 2 2 : -0 -0 -2 -2 -2
</span></pre>
Reference: <a href="http://en.cppreference.com/w/cpp/numeric/math/nearbyint">http://en.cppreference.com/w/cpp/numeric/math/nearbyint</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0tag:blogger.com,1999:blog-6646285475339095415.post-51043696405015234542017-12-11T17:10:00.003-05:002017-12-11T17:10:57.474-05:00C++11: std::copysign() and std::signbit()C++11 added the functions std::copysign() and std::signbit(). Here is an example:
<br />
<pre style="line-height: 1em;"><span style="font-size: x-small;">
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << copysign(-11.0, 99.0) << " "; // 11
cout << copysign(-11.0, -99.0) << " "; // -11
cout << copysign( 11.0, 99.0) << " "; // 11
cout << copysign( 11.0, -99.0) << " "; // -11
cout << ": ";
cout << signbit( 0.0) << " "; // 0
cout << signbit(-0.0) << " "; // 1
cout << signbit( 9.0) << " "; // 0
cout << signbit(-9.0) << " "; // 1
cout << endl;
return 0;
}
// Output: 11 -11 11 -11 : 0 1 0 1
</span></pre>
References:<br />
<a href="http://en.cppreference.com/w/cpp/numeric/math/copysign">http://en.cppreference.com/w/cpp/numeric/math/copysign</a><br />
<a href="http://en.cppreference.com/w/cpp/numeric/math/signbit">http://en.cppreference.com/w/cpp/numeric/math/signbit</a>C++ Triviahttp://www.blogger.com/profile/13687133438732833204noreply@blogger.com0