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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:08:51+00:00 2026-06-14T12:08:51+00:00

Hello I’m trying to create a genetic algorithm with C++ and I tried to

  • 0

Hello I’m trying to create a genetic algorithm with C++ and I tried to use vector as the container the problem is I didn’t know how to set the size of the vector because the vector have a class argument like this

class population
{
    friend class chromosome;
private:
    int population_number;
    int best_index[2];
    vector <chromosome *> chromosome_population;
public:
    population(int numberOfPopulation);
    population(int numberOfPopulation,int numberOfSynapse);
    ~population();
    int worst_chromosome();
    void mating();
    void crossover(int parent_1,int parent_2);
};

this is the population class and here’s the chromosome class

class chromosome
{
    friend class population;
private:
    int chromosome_id;
    float fitness;
    vector <gen *> gen_chromosome;
public:
    chromosome();
    ~chromosome();
    void fitness_function();
    void mutation_translocation();
    int get_chromosome_size();
};

how can I set the vector length in the population class constructor? I’ve tried to use vector.pushback and vector.resize but both will give me error because the argument doesn’t match. Actually I’m understand why it become error but I didn’t know how to match the argument inside the vector pushback here’s my population constructor

population::population(int numberOfPopulation)
{
    srand (time(NULL));
    population_number = numberOfPopulation;
    for(int a=0;a<population_number;a++)
    {
        chromosome_population.push_back();
    }
    cout<<chromosome_population.size();
    for(int i=0;i<population_number;i++)
    {
        chromosome_population[i]->chromosome_id = i;
        int chromosome_length = rand() % 10 + 1;
        for(int j=0;j<chromosome_length;j++)
        {
            chromosome_population[i]->gen_chromosome[j]->basa_biner = rand()%1;
            chromosome_population[i]->fitness = (rand()%99)+1;
        }
    }
}

If is there any other information you want you can tell me in the comment and I’ll add the information you needed. Thanks before.

  • 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-14T12:08:52+00:00Added an answer on June 14, 2026 at 12:08 pm

    std::vector has several constructors and one of the variants accepts the initial number of elements to be stored in the vector.

    Specify the size of the vector in the population constructor’s initializer list:

    population::population(int numberOfPopulation) :
        population_number(numberOfPopulation),
        chromosome_population(numberOfPopulation)
    {
    }
    

    Give this approach, the population_number member variable is unnecessary as it can be obtained by chromosome_population.size().

    Specifying an initial size on the vector will mean that it contains numberOfPopulation null pointers. Before accessing elements in the vector you need to create objects, in this case using new. If the elements are copyable and polymorphic behaviour is not required then recommend using vector<chromosome> instead. If you must use dynamically allocated elements in the vector then you must allocate first:

    chromosome_population[i] = new chromosome();
    

    and remember to delete when no longer required.

    It also desirable to use a form of smart pointer instead of raw pointers. An advantage of using the smart pointer is that when the vector<unique_ptr<chromosome>> goes out of scope the elements will be destructed for you, without having to explicitly call delete on each of the elements. See What C++ Smart Pointer Implementations are available? for a useful listing of the available smart pointers.

    Note that vector::push_back() accepts an argument, with same type as its element. So the correct invocation of push_back() is:

    chromosome_population.push_back(new chromosome());
    

    If you specify an initial size of the vector at construction, calling push_back() will add elements after the initial (null pointers in this case) elements in the vector.

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

Sidebar

Related Questions

Hello I am trying to use TCL in a program called Shorthand which is
Hello I am trying to lock a file by using file.flock(File::LOOK_EX) The problem it
Hello ! I need to create a report like the attachment shows with Jasper
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
Hello and thank you in advance. I know this is total noob question, and
Hello All I am trying to flatten a list in Ocaml. I am a
Hello I want to create a table view like as following image There is
Hello everyone I use Excel 2010 with VB Ok so I have an issue
Hello I am trying to connect my c# application I used this code I

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.