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 6562087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:40:04+00:00 2026-05-25T13:40:04+00:00

In my question a vector means a vector of structures of the form struct

  • 0

In my question a vector means a vector of structures of the form

struct node 
      {string key; 
       int a,b; };

I want to code up the following: Given a user specified number say d I want to create a sequence of d vectors v_1, v_2, ...v_d.

v_1 is created from user data, and v_i from v_(i-1) for i>=2 .
The size of v_i is the number of nodes in v_(i-1) with distinct key fields.
The a and b fields of the v_i nodes are calculated according to some black box algorithm from those of v(i-1)

How should I code this up efficiently in C++?
Should I use a map of the form
map<int, vector<nodes>> where the int field varies between 1 and d??

Thank you!

P:S: I guess one could code up a solution, using new operator and some pointer gymnastics, instead of using vectors. But I would like to have as pointer free a solution as possible.

  • 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-25T13:40:04+00:00Added an answer on May 25, 2026 at 1:40 pm

    Use std::vector<std::vector<node> >, but use vector::reserve() intelligently so as to avoid reallocations:

    using std::vector;
    vector<vector<node>> v(d); // 0 nodes allocated, no wasted space yet
    
    using std::copy;
    using std::istream_iterator;
    using std::back_inserter;
    // Wish I could v[0].reserve(), but we don't have that info yet.
    copy(istream_iterator<node>(std::cin), istream_iterator<node>(), back_inserter(v[0]));
    
    // foreach v[1..d-1], compute v[i] from v[i-1]
    for(int i = 1; i < d; ++i) {
        int size = ComputeSize(v[i-1]); // how big a list do we need?
        v[i].reserve(size);  // reserve the space for it, and 
        PopulateVectorFromVector(v[i-1], v[i]); // fill it in.
    }
    

    The only vector with wasted space and time is v[0].


    EDIT: the call to std::copy above is a loop-less idiom roughly equivalent to

    node tmp;
    while(std::cin >> tmp) {
        v[0].push_back(tmp);
    }
    

    std::copy takes three arguments. The first two are input iterators which define the range from which we copy, while the third is an output iterator defining the destination.

    Specifically, istream_iterator<node>(std::cin) is an input iterator which, when dereferenced, reads the next node from the standard input stream. back_inserter(v[0]) is an output iterator which, when dereferenced and assigned to, invokes v[0].push_back().

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

Sidebar

Related Questions

Question Using XSLT 1.0, given a string with arbitrary characters how can I get
In the following code Vector v = new Vector(); v.add('c'); v.add('c') is allowed in
My question is simple: are std::vector elements guaranteed to be contiguous? In other words,
Just a simple question: if I had a simple vector class: class Vector {
Question is pretty self explanitory. I want to do a simple find and replace,
My question is how the following line can be parsed as a function declaration:
On a different question , the user that answered (Ricky) me also said the
In a data-structure, how do you insert a function? struct Student_info { std::string name;
I wrote a storable vector instance for the data type below ( original question
As part of a larger function, I'm writing some code to generate a vector/matrix

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.