Articles & Books

Quick Q:How to modify a tuple in a vector of tuples c++?

Quick A: Capture the tuple by reference.

Recently on SO:

Modifying a tuple in a vector of tuples c++

I have a vector of tuples vector<tuple<int,int>> vector; and I want to modify one of the tuples it contains.

for (std::tuple<int, int> tup : std::vector)
{
    if (get<0>(tup) == k)
    {
        /* change get<1>(tup) to a new value
         * and have that change shown in the vector
         */
    }
}

I am unsure how to change the value of the tuple and have the change be reflected in the vector. I have tried using

get<1>(tup) = v;

but that doesn't change the value of the tuple that is in the vector. How can I do this? Thanks.

How to write a standard-like algorithm -- Indi

Explicit C++ has posted a nice tutorial on how to implement an algorithm in C++.

How to write a standard-like algorithm

by Indi

from the article:

Writing a standard-like algorithm should be one of the key parts of a modern C++ beginner’s course outline. This post will be a whirlwind guide through the steps toward creating a standard-like algorithm. The focus is not on the algorithm itself, but on the process of creating one.

Iterators++, Part 3 -- Eric Niebler

Eric Niebler concludes his series about proxy iterators with:

Iterators++, Part 3

by Eric Niebler

From the article:

This is the forth and final post in a series about proxy iterators, the limitations of the existing STL iterator concept hierarchy, and what could be done about it. The first three posts describe the problems of proxy iterators, the way to swap and move their elements, and how to rigorously define what an Iterator is.

This time around I’ll be focusing on the final problem: how to properly constrain the higher-order algorithms so that they work with proxy iterators.

A conditional copy constructor -- Andrzej Krzemieński

Andrzej writes in his recent blog about an issue library writers have to take care of.

A conditional copy constructor

by Andrzej Krzemieński

From the article:

In this post we will try to define a ‘wrapper’ class template that does or does not have a copy constructor depending on whether the wrapped class has it. This will be a good opportunity to explore in depth a couple of advanced C++ features. Note that this is a rather advanced topic and, unless you are writing or maintaining a generic library, you will probably never need this knowledge.

Vector Hosted Lists--Thomas Young

Want perfomance and speed? Vectors are the solution:

Vector Hosted Lists

by Thomas Young

From the article:

Vectors are great when adding or removing elements at the end of a sequence, but not so hot when deleting elements at arbitrary positions.

If that's a requirement, you might find yourself reaching for a pointer-based list.

Not so fast!

Memory locality is important, contiguous buffers are a really good thing, and a standard vector will often out-perform pointer-based lists even where you perform non-contiguous, list-style modifications such as arbitrary element deletion.

And we can 'host' a list within a vector to get the advantages of a contiguous buffer at the same time as 0(1) complexity for these kinds of manipulations...

Comparison of C++ Format and C library's printf -- Victor Zverovich

The C printf family is still the gold standard of performance for formatted I/O, but woefully fragile and inextensible. C++'s iostreams offers important advantages in type safety, but struggles with performance and internationalization.

A number of projects, such as Boost.Format, try to the advantages of both. Here's further discussion about one such library we linked to recently:

Comparison of C++ Format and C library's printf

by Victor Zverovich

From the article:

I was recently asked on GitHub why would someone use fmt::printf, which is a part of the C++ Format library, over the standard C library printf. This is a fair question and so I decided to write this blog post comparing the two.

Disclaimer: I’m the author of C++ Format, so this is all very biased =).

Operator Overloading – Introduction to Boost.Operators -- Arne Metz

Arne Mertz goes into the details of using boost operators.

Operator Overloading – Introduction to Boost.Operators, Part 1

Operator Overloading – Introduction to Boost.Operators, Part 2

by Arne Mertz

From the articles:

In my first two posts about operator overloading I have written about the basics and common practice. This post shows some lessons from the common practice post on a concrete example and then introduces to Boost.Operators, a library that conveniently reduces the boilerplate involved when overloading multiple operators for a class.

Operators Travel in Packs

If we look at the list of operators, we see that there are about 50 of them, and many of them can be overloaded in different ways. Even if we restrict ourselves to a few operations that make sense for a given class, then one of those operations often brings two or more operators.

 

SFINAE std::result_of? Yeah, right!--Scott Prager

On the matter of having a useful error message:

SFINAE std::result_of? Yeah, right!

by Scott Prager

From the article:

Return type deduction pre-decltype and auto could really make one mad. Back then, if you wanted a function object, you had to make it "adaptable", meaning it had to inherit from std::unary_ or binary_function and define its first_argument_type, second_argument_type, and result_type. There had been no concept of "transparent functors" allowing us to pass polymorphic functions to higher order functions (like std::plus<> to std::accumulate). For an example of programming in these dark ages, check out the FC++ FAQ.

 

Type Deduction and Braced Initializers--Arne Mertz

If you are confused about the topic, read this article:

Type Deduction and Braced Initializers

by Arne Mertz

From the article:

I just finished watching a talk from CppCon 2014 by Scott Meyers: Type Deduction and Why You Care. All in all it was a very interesting and entertaining talk, and I learned a thing or two, especially about the combination of type deduction and braced initializers. Since this blog is about simplifying the use of C++, I want to have a short look at that special combination and derive a rule of thumb from it...