How can I print a vector in a recursive function?

Syfu_H

I have a problem here: I am to write a function that prints the elements of a vector recursively so no loop is allowed.

I tried this code but it crashes at runtime:

void print(const std::vector<int> ivec, std::vector<int>::const_iterator it) {
    if (it == ivec.end())
        return;
    std::cout << *it++ << std::endl;
    print(ivec, it);
}

int main(){

    vector<int> v{
        5, 7, 77, 23, 10, 81
    };

    print(v, v.begin());
}

If I run the program I get the assertion dialog!?

Sam Varshavchik
void print(const std::vector<int> ivec, std::vector<int>::const_iterator it) {

The ivec parameter is passed by value. Both of these parameters are passed by value. Passing by value by means that inside the function these are copies of the original parameters.

Your main() calls this recursive function passing it its vector and the beginning iterator of its vector. However because all parameters are passed by value, each recursive iteration of the function compares the iterator to the end() of a completely different vector. Undefined behavior.

You obviously forgot to pass the vector by reference. The first parameter to should be const std::vector<int> &ivec.

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

How can i make this factorial function recursive?

how i can get index in recursive function in javascript?

How can I extract (values of yes) in vector from ifelse function

How can I flush the output of the print function (unbuffer python output)?

How can I print a Numpy Array as a result from a function?

How can I deal with compiler error c2660 in recursive function and iterative function?

R: How to output a recursive or iterative function/map output into a vector?

How to create a recursive function to print a multilevel dictionary object to a file

How can I print cPacket?

How can I pull a group-based vector to pass to a function within dplyr's summarize or mutate?

how can I use a function to print the name of args not use define In c++

How can I write a function that doesn't print out uppercase words from the list?

How do I print the elements of a C++ vector in GDB?

How can I change the indices of an R vector?

How can I make a column vector?

How can i create a vector of the form?

How Can I Make A Vector of Vectors in R

How can I print multidimensional arrays in C?

How can I print the outliers of a Boxplot in Python?

How can I suppress (not print) line numbers?

How can I print subelements in Python?

How can I print objects to the console?

How can I return vector<vector<float>> from JNI?

Recursive function in C to print 1 to n to 1

Recursive sum function, How do i limit the sum?

How can I print numbers between two numbers that I enter?

How can a function append a value to a vector and also return that value?

How do I put this code into one print function (or line)?

How do I add the result of a print function ito a list