Tags down


Replace N-th element of a std::tuple

By : Anthony Beaudoin-Sco
Date : September 14 2020, 07:00 PM
wish of those help What is the shortest / best way to replace the n-th element of a tuple with a value (which may or may not have a different type)? Solutions including c++20 are fine. [EDIT: I would prefer something not requiring other libraries, but I'm still interested what solutions are possible with e.g. boost]. , One solution I found for c++20 is this:
code :
#include <cassert>
#include <tuple>
#include <type_traits>

template<std::size_t N, class TupleT, class NewT>
constexpr auto replace_tuple_element( const TupleT& t, const NewT& n )
    constexpr auto tail_size = std::tuple_size<TupleT>::value - N - 1;

    return [&]<std::size_t... I_head, std::size_t... I_tail>
        ( std::index_sequence<I_head...>, std::index_sequence<I_tail...> )
            return std::tuple{
                std::get<I_head>( t )...,
                std::get<I_tail + N + 1>( t )...

struct Foo {
    int value;

int main()
    auto t1  = std::tuple{ 0, 1, 2, 3 };
    auto t2 = replace_tuple_element<2>( t1, Foo{10} );

    assert( std::get<0>(t2) == std::get<0>(t1));
    assert( std::get<1>(t2) == std::get<1>(t1));
    assert( std::get<2>(t2).value == 10);
    assert( std::get<3>(t2) == std::get<3>(t1));

Share : facebook icon twitter icon

replace element from a tuple of tuples with empty

By : Мервне Манаа
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , This my schema for the tuple:
code :
In [13]: UserList = tuple((n, a, w) if a >= 18 else ('', None, None) for (n, a, w) in UserList)

In [14]: UserList
Out[14]: (('', None, None), ('Mike', 19, 178), ('Pull', 24, 200), ('', None, None))

How do I replace a tuple element at compile time?

By : Kris Carr
Date : March 29 2020, 07:55 AM
hop of those help? Is there a way to replace a tuple element at compile time? , You can use this:
code :
// the usual helpers (BTW: I wish these would be standardized!!)
template< std::size_t... Ns >
struct indices
    typedef indices< Ns..., sizeof...( Ns ) > next;

template< std::size_t N >
struct make_indices
    typedef typename make_indices< N - 1 >::type::next type;

struct make_indices< 0 >
    typedef indices<> type;

// and now we use them
template< typename Tuple, std::size_t N, typename T,
          typename Indices = typename make_indices< std::tuple_size< Tuple >::value >::type >
struct element_replace;

template< typename... Ts, std::size_t N, typename T, std::size_t... Ns >
struct element_replace< std::tuple< Ts... >, N, T, indices< Ns... > >
    typedef std::tuple< typename std::conditional< Ns == N, T, Ts >::type... > type;
using a_t = std::tuple<std::string,unsigned>;     // start with some n-tuple
using b_t = element_replace<a_t,1,double>::type;  // std::tuple<std::string,double>
using c_t = element_replace<b_t,0,char>::type;    // std::tuple<char,double>

For tuple in list, if 3rd element of tuple is in dicionary, replace the element with the value

By : hugo torres
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a list of tuples: , you can simplifiy it down to a list expression:
code :
[(x[0], x[1], b.get(x[2], x[2])) for x in a]

Matching partial specialization to replace tuple element by type

By : Raja Atif Nasir
Date : March 29 2020, 07:55 AM
Does that help One step at a time.
First, use specialization to map one type to another, leaving all other types unchanged. If T is ToRemove, replace it with `ToReplace, else leave it alone:
code :
template<typename T, typename ToRemove, typename ToReplace>
struct replace_type {

    using type=T;

template<typename ToRemove, typename ToReplace>
struct replace_type<ToRemove, ToRemove, ToReplace> {

    using type=ToReplace;
template<class Tuple, typename ToRemove, typename ToReplace>
struct replace_tuple;

template<typename ...Args, typename ToRemove, typename ToReplace>
struct replace_tuple<std::tuple<Args...>, ToRemove, ToReplace> {

    using type=std::tuple<typename
                  replace_type<Args, ToRemove, ToReplace>::type
replace_tuple<std::tuple<int, char, int>, char, unsigned>::type foo;

std::tuple<int, unsigned, int> *bar=&foo;

Replace first element in every tuple of a list with a number DrRacket Scheme

By : Graham
Date : March 29 2020, 07:55 AM
will help you It's not very clear if the numbers gets replaced with the letters or vice versa, but it's all the same. You need to make one procedure that does it for one tuple:
code :
(define (letter->number tuple)
   (case (car tuple)
     ((R) 'first)
     ((p) 'second)
     (else 'third))
   (cdr tuple)))

(letter->number '(R . 1)) ; ==> (first . 1)
(letter->number '(p . 1)) ; ==> (second . 1)
(letter->number '(t . 1)) ; ==> (third . 1)
(map letter->number '((R . 1) (R . 5) (R . 2) (R . 4) (p . 3) (t . 6) (R . 7)))
; ==> ((first . 1) (first . 5) (first . 2) (first . 4) (second . 3) (third . 6) (first . 7))
Related Posts Related Posts :
  • C++ template function resolution in splitted namespaces
  • I am trying to swap two values by pass by reference there is no error occur but the compiler throw some message
  • How could I obtain the common keys of two std::map?
  • Default argument in struct c++
  • How to make a variable cycle through range?
  • C++ sum of vector values (recursive)
  • How do I proceed with a question like this?
  • If the A class's member function is changed, client binary of the A class need to be rebuild?
  • Capture arguments are copied from rvalue lambda?
  • C++ program not running on windows systems without VS installed "VCRUNTIME140.dll was not found"
  • Know if template parameter is const char* at compile time?
  • Return value changes partially and deterministically
  • How to use typedef to create multiple arrays?
  • How to get to the Nth node in the Linked list?
  • Warning: "variable may be used uninitialized in this function" in a multi-mode structured function
  • I am trying to print some corresponding values to a character but condition always going to else
  • Can I generate 64-Bit application binary from a 32-Bit GCC compiler?
  • Restinction on values for operator+ overload in templated class
  • C++: array of vectors
  • Hackerrank: Day 1: Data Types in C++
  • How to insert elements into shared pointer of std::map?
  • Is an initializer list static enough to allow instantiating templates?
  • Minimize button won't appear on MFC Dialog
  • Why does const prevent this from compiling?
  • Averaging ADC readings have strange steps in plot
  • Rock,Paper, Scissors Program Game
  • Why pow(10,2)=99 & pow(10,3)=1000?
  • What is the lifetime of C++ member variables when running in a std::thread?
  • C++ | Print tree (not necessarily binary) in a pretty way to stdout
  • The difference between delete and the destructor when both called?
  • What happens inside of a C++ constructor?
  • Mixing overriding and overloading in C++
  • Is std::stringstream::flush() supposed to do anything?
  • How does Qt::DirectConnection act when used in multi-threaded environment?
  • bison/flex: calculator interprets double values as integers so I added #define YYSTYPE double, but I multiple compilatio
  • Register the environment varibles for TBB
  • modelo of square is zero, factorial modelo problem
  • Split student list that has format like: 0001 William Bill Junior 8.5
  • How to use the overloaded "operator<<" properly?
  • Why Smart pointers can not be declared the usual Pointer way
  • I can't figure out what is wrong my implementation of MST with Kruskal algorithm
  • C++ how would one simplify this template so that it is not specialized?
  • What happens when a reference of an object (with different types) is deleted?
  • Is main() always on the stack?
  • What if one process in a MPI program proceeds more slowly than others?
  • I'm trying to get current date and time in cpp but can't understand these lines
  • make_pair() in C++
  • How can I access to derived members from a pure virtual base class function?
  • Constructor cannot be redeclared. Why?
  • Is there any similar function to glibc mtrace() function in c++?
  • faster erase-remove idiom when I don't care about order and don't have duplicates?
  • How to store variable as nothing
  • How to properly free a set of pointers?
  • C++ file IO - ASCII value instead of Binary value
  • Weird behavior of a simple for-loop code using "find" function in C++(to increment value if a certain sub-stri
  • I want two loops to run in parallel
  • Why would it be good to use dynamic memory allocation with serial communication?
  • non static member in static cpp
  • Non type template parameter
  • 3x3 Matrix Rotation with non uniform scaling
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk