C++ vector traversal demo, you can choose a variety of poses ~

1. The iterator

    for(vector<int>::iterator it = obj.begin(a); it ! = obj.end(a); it++) { cout << *it <<",";
    }
Copy the code

2. Add the auto keyword in C++11

    for(auto it = obj.begin(a); it ! = obj.end(a); it++) { cout << *it <<",";
    }
Copy the code

3. Array traversal

    for(int i=0; i<obj.size(a); i++) { cout<<obj[i]<<",";
    }
Copy the code

4. Scope-based for loops

    for (auto iter : obj) 
    {
        cout << iter << ",";
    }
Copy the code

The demo sample

#include <vector>
#include <iostream>
using namespace std;
 
int main(a)
{
    vector<int>obj;
    
    for(int i=0; i<10; i++) { obj.push_back(i);
        cout<<obj[i]<<",";    
    }
    cout << endl << "vector size is :" << obj.size() << endl;
    
    / / the iterator traverses
    for(vector<int>::iterator it = obj.begin(a); it ! = obj.end(a); it++) { cout << *it <<",";
    }
    cout << endl;
    
    // Loop through the array
    for(int i=0; i<obj.size(a); i++) { cout<<obj[i]<<",";
    }
    cout << endl;
    
     // The auto keyword traversal traversal
    for(auto it = obj.begin(a); it ! = obj.end(a); it++) { cout << *it <<",";
    }
    cout << endl;
	
	// Scope based for loop
    for (auto iter : obj) 
    {
        cout << iter << ",";
    }
    cout << endl;
 
    return 0;
}
Copy the code

Results:

The end of the message

That concludes the small details of vector. I’ll see you in the next post

It is not easy to write a blog, such as being loved, appreciate a concern, one key three connect ~~ like + comment + collection 🤞🤞🤞, thank you for your support ~~Copy the code