Vector class and random numbers in C++

Asked

Viewed 744 times

0

The function only returns me the memory address, how do I make it return the content and not the memory address?

#include <iostream>
#include <time.h>  ///  por causa de srand()
#include <stdlib.h>   // para poder gerar os pesos aleatorios ou pra poder usar o rand()
#include <vector>   /// vector que armazenara os meus neuroneos
#include <deque>
#include <iomanip>   // por causa do setw()

int* pes;

void funcaoGerarPesos(){   /// sempre por referencia

    int colunas = 10;

    /// lembre-se que o numero de pesos é sempre igual ao tamanho de exemplo de treinamento
    for (j = 0; j < colunas; ++j)
    {
        srand(time(NULL));     //// responsavel pela geracao automatica

        peso_aleatorio = (rand() % 6) + 1;    //// armazenar o valor gerado na variavel valor_treinamento

        pes = &peso_aleatorio;    //// o meu ponteiro apontara para o valor contido na variavel peso_aleatorio

        vector_pesos.push_back(pes);    //// pegar todos os valores gerados e colocar no vector de pesos
    }

    //// percorrer com o while
    it = vector_pesos.begin();

    while(it != vector_pesos.end()){

        cout << *it << endl;
        it++;
    }
}


int main(int argc, char *argv[]){

    funcaoGerarPesos();
}
  • Hello, the function only returns to me the memory address, as I do that it returns to me the content and not the memory address, I would appreciate if someone could help, any help will be welcome, thanks since

  • 1

    Please edit your question to include in it that explanation you provided in comment. Also include other things that are missing, for example: (1) where your variable is declared vector_pesos? (2) Why are you adding in the vector a pointer and not the value directly? (3) Why didn’t you make your function directly return a vector<float> instead of void?

1 answer

-1

Answering the last question asked in the comments (Since you already found the problem).

#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>

int main(int argc, char **argv)
{
    std::vector<int> v;
    srand(time(NULL));
    for (int i = 0; i < 10; i++)
    {
        // O int e o ponteiro para int precisam ser redeclarados e redefinidos a cada iteração.
        int r = (rand() % 6) + 1;
        int *p;
        p = &r;
        v.push_back(*p);
    }
    std::vector<int>::iterator it = v.begin();
    while(it != v.end())
    {
        std::cout << *it << std::endl;
        it++;
    }
    return 0;
}

As you can see in my example, for random numbers not to be repeated, it is necessary to redeclare and reset the int and pointer to int within the scope of for-loop.

Remember that if your compiler supports the 2011 C++ standard, you can iterate over the loop in a much simpler way:

for (auto e : v)
{
    std::cout << e << std::endl;
}
  • The problem is not that, this function I call within my main function, only that it does not from error, just returns the position of memory, and that is not what I want, I want the value for each position, but thank you

  • Which function only returns the "memory address"? It is not evident in your question. I tested it here and it works perfectly. I need more details. Where was this pointer declared? Edit the question to add the details.

  • I’ll put in the full code then

  • I’m sorry, it just got a little messy, but this is the one

  • I have tested all the code and it works perfectly rs.. The random numbers are stored in the vector, and are "printable" with Std::Cout. No address.

  • I think I’ve found the solution, I just changed the type my vector received from int* to int and tm in the case of iterator

  • is that I’m in Ubuntu, maybe that’s why, but I honestly don’t know if that’s why, but thank you

  • but how do I so that my vector does not generate repeated numbers

  • I edited my answer and answered your last question (If you solve your problem, do not forget to accept my answer as correct rsrsrs..).

  • no problem

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.