Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:16:43+00:00 2026-06-17T20:16:43+00:00

Say I have a vector of some struct type; I’m trying to iterate over

  • 0

Say I have a vector of some struct type; I’m trying to iterate over all instances of a specific member within this struct. Because I want to be general in my approach I’d like to use a std::function<> object to specify which piece of information I’d like to access. I build the following template class

#include <iostream>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
#include <cmath>

template <typename SrcList, typename Tgt>
class Access
{
    typedef std::function<Tgt &(typename SrcList::value_type &)> func_type;
    typedef typename SrcList::iterator src_iterator;
    typedef Tgt value_type;

    SrcList &source;
    func_type f;

    public:
        Access(SrcList &source_, func_type const &f_):
            source(source_), f(f_) {}

        class iterator: 
            public src_iterator,
            public std::iterator<std::forward_iterator_tag, value_type>
        {
            Access const *obj;

            public:
                iterator(Access const *obj_, src_iterator i):
                    src_iterator(i), 
                    obj(obj_)
                {}

                value_type &operator*()
                {
                    return (obj->f)(src_iterator::operator*());
                }
        };

        value_type &operator[](size_t i)
        { return f(source[i]); }

        iterator begin()
        { return iterator(this, source.begin()); }

        iterator end()
        { return iterator(this, source.end()); }
};

next we define a struct S, and a main function to test the class

struct S
{
    double v[3];
};

std::ostream &operator<<(std::ostream &out, S const &s)
{
    return out << s.v[0] << " " << s.v[1] << " " << s.v[2];
}

int main()
{
    std::vector<int> A = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<S> Q(10);

    for (unsigned k = 0; k < 3; ++k)
    {
        Access<std::vector<S>, double> acc(Q,
            [k] (S &s) -> double&
        {
            return s.v[k];
        });

        std::transform(A.begin(), A.end(), acc.begin(),
            [k] (int i)
        {
            return pow(i, k+1);
        });

        for (auto x : Q)
            std::cout << x.v[k] << std::endl;
        std::cout << "--- end of part " << k << " ---\n";
    }
    std::cout << std::endl;

    for (auto x : Q)
        std::cout << x << std::endl;

    return 0;
}

this program should print the numbers 0..10, their squares and their cubes. The method seems to work, however I get a memory dump, right after printing “— end of part 1 —“, saying: “double free or corruption”. I’ve ran the code through gdb, and it seems somethings going wrong in the memory management of the std::function<> member in Access<>, but I can’t figure what precisely breaks this code. I’ve build a similar construct with read-only access, and it works flawlessly.

What am I doing wrong here?

(using g++-4.7.2)

Cheers, Johan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:16:44+00:00Added an answer on June 17, 2026 at 8:16 pm

    The problem is that vector A contains 11 entries, while vector Q contains only 10. std::transform<> expects the target range to be at least as big as the source range.

    A simple fix would be to change the definition of Q as follows:

    std::vector<S> Q(A.size());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a simple class with some storage data structure (list, vector, queue,
Say that I have some user-defined complex struct, like struct usrStruct{ double a; T1
Say we have some vector: someVector = c(1, 3, 4, 6, 3, 9, 2,
Let's say I have a std::vector and I get by some means the address
Let's imagine we have a struct for holding 3 doubles with some member functions:
Let's say I have a bunch of vector types (a la XNA) and some
Let's say I have a vector class: typedef struct vec3_s { float x, y,
Let's say I have a class that includes an array of some struct, and
Let's say I have sampled some signals and constucted a vector of the samples
Say I have a vector: (def data [Hello World Test This]) And I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.