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

  • Home
  • SEARCH
  • 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 6664291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:35:39+00:00 2026-05-26T02:35:39+00:00

Below is a slightly modified code from one good example how to copy values

  • 0

Below is a slightly modified code from one good example how to copy values fro one vector of strings to another vector of objects. (see: another copy algorithm )

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;

class Object
{
public:
    std::string Name;

    Object(const std::string& Name_ = "")
        : Name(Name_)
    {
    }
};

int main()
{
    //std::vector<Object> objects(3, Object());
    std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

    std::vector<std::string> names;
    names.push_back("Alpha");
    names.push_back("Beta");
    names.push_back("Gamma");
    std::vector<std::string>::const_iterator names_it;
    names_it = static_cast<const std::vector<std::string>&>(names).begin();

    //std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
    std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

    //std::vector<Object>::iterator it, end = objects.end();
    std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
    for (it = objects.begin(); it != end; ++it) {
        //std::cout << it->Name << std::endl;
        std::cout << (*it)->Name << std::endl;
    }

    return EXIT_SUCCESS;
}

In this case I’m using dynamically allocated Objects, and because boost::lambda::bind can’t handle such changes as boost::bind do, I need to dereference placeholder in order to compile:

std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

But then at the output I’ve got:

Gamma
Gamma
Gamma

What is your explanation?

  • 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-05-26T02:35:40+00:00Added an answer on May 26, 2026 at 2:35 am
    std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());
    

    This is equivalent to doing:

    shared_ptr<Object> object = make_shared<Object>();
    std::vector<shared_ptr<Object> > objects(3, object);
    

    The vector constructor will then make 3 copies of that object pointer, which will all refer to same one and unique Object (that is to say, *object). Fill the vector with separate pointers each initialized with their own Object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some code on the internet as below (slightly modified). It simply requests
I'm using a slightly modified ImageAndTextCell from one of Apple's tutorials in a custom
This is a slightly modified example from the Java Sound info page. https://stackoverflow.com/tags/javasound/info Unfortunately,
The code below has me slightly perplexed: function(__m128 foo) { __m128 bar = _mm_shuffle_ps(foo,
Below is my VHost (which is slightly modified to obscure some URLS): 1 NameVirtualHost
EDIT, Changed the code slightly based on answers below, but still haven't got it
Answer, slightly modified from marked answer: var eoc = easeOutCirc,mNSS = 'metrsoNavSpanSelected'; $('.metroNavSpan').click(function(){ var
Below I have a very simple example of what I'm trying to do. I
Below is my $.ajax call, how do I put a selects (multiple) selected values
Below is the code of a simple html with a table layout. In FF

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.