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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:35:37+00:00 2026-06-10T21:35:37+00:00

I have a 2D vector as defined by the classes below. Note that I’ve

  • 0

I have a 2D vector as defined by the classes below. Note that I’ve used classes because I’m trying to program a genetic algorithm such that many, many 2D vectors will be created and they will all be different.

class Quad: public std::vector<int>
{
    public:
    Quad() : std::vector<int>(4,0) {}
};

class QuadVec : public std::vector<Quad>
{
};

An important part of my algorithm, however, is that I need to be able to “mutate” (randomly change) particular values in a certain number of randomly chosen 2D vectors. This has me stumped. I can easily write code to randomly select the value within the 2D vector that will be selected for “mutation” but how do I actually enact that change using classes? I understand how this would be done with one 2D vector that has already been initialized but how do I do this if it hasn’t?

Please let me know if I haven’t provided enough info or am not clear as I tend to do that.

Thanks for your time and help!

  • 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-10T21:35:38+00:00Added an answer on June 10, 2026 at 9:35 pm

    As already noted in the comments, inheriting from std::vector is considered bad practice in C++ unless there is some extremely rare case you need it for (please read the link for more details). From your question, the following is possibly what you are looking for:

    typedef std::vector<std::vector<int>> twodvec; // Create a typedef
    twodvec vec1(10, std::vector<int>(4, 0)); // Create twodvec which holds 10 quads
    twodvec vec2(20, std::vector<int>(4, 0)); // Create twodvec which holds 20 quads
    vec1[0][0] = 1; // Use subscripts to assign values to the preallocated memory
    vec2[0][1] = 2;
    

    Use push_back if you haven’t preallocated the memory. Since your class does not seem to add any extra functionality, it seems all you need to do is something similar to the above.

    To update the answer showing composition:

    class QuadVec
    {
        std::vector<std::vector<int>> twodvec; // private
    public:
        QuadVec() : twodvec(10, std::vector<int>(4, 0))
        {}
        int& operator()(std::size_t row, std::size_t col)
        {
            // checking for out-of-bound indexes would be good to
            return twodvec[row][col];
        }
     };
    

    Then update the QuadVec like this:

    QuadVec v; 
    v(0, 0) = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey, I have this code that should save a java.util.Vector of custom serializable classes:
I have a vector class and I defined the __mul__ method to multiply a
I have a vector of objects (objects are term nodes that amongst other fields
I have the vector output = PV_out(:); I am trying to break this down
I have a class Message that has a std::string as a data member, defined
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in
I have a problem with the definitions of my classes, probably because of the
Suppose I have a Vector datatype defined as follows: data Vector = Vector {
I am working in cross platform C++, and have some classes defined like so:
I have a bunch of classes which implement geometric objects based on vertices, such

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.