C++ Tutorials

C++ Tutorials

using namespace std;

int main(){
    vector<vector<int>> v {{1, 0, 1}, {0, 1}, {1, 0, 1}}; 
    for(int i=0;i<v.size();i++){
        for(int j=0;j<v[i].size();j++)
            cout<<v[i][j]<<" ";

1 0 1 
0 1 
1 0 1

Learn how to read a file into a container vector in C++ I already included <iostream> stream vector and iterator.

And now in our main function first i’m going to create a if stream file object and pass in the file name that we want to read it i have this file in file dot txt it has some content few lines stream file.

And i will read this file in dot txt now we have his stream object i’m going to create a vector of type string and we and then I will have another variable of type string in which we are going to read the lines from the file over here.

I’ll add stranger variable STR and I will have a loop in this loop I will read the content from a file and to overstrained and then add 12 vector by using a function push back and pausing our strength.

So we are going to loop through each line and push into our vector and at the end of the while loop or wenda we are and reading the file.

I’m going to print the content of our vector by using a coffee method and pass in we dot again this is a way to print a content of a vector V dot N.

And then I will pass in Oh a stream stream and it’s it iterator and for the iterator we need to tell the type we are going to iterate through it’s gonna be a string type.

And then I’m going to pass in our see out and hover delimiter that will be a new line for us.

So we are going to separate the content by the new line and just a semicolon here and going to save it and run another program our rector name is we so it’s gonna be pushback and here is the output and we are able to read that far into our vector and also print the content of our vector.

Initialize 2d Vector C++

#include <iostream>
#include <vector>

std::vector<std::vector<int> > fog(
std::vector<std::vector<int> > fog(
    std::vector<int>(ROW_COUNT, 4));
using {}:

std::vector<std::vector<int> > fog { { 1, 1, 1 },
                                    { 2, 2, 2 } };
Try this:

std::vector<std::vector<int>> twoDimVector(3, std::vector<int>(2, 0));

2d vector c++ input

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
for(i=0; i<in; i++){ 
    cout<<"ship"<<i+1<<":"<<' ';
    vector<int> row;
    for(j=0; j<in; j++){
      row.push_back(temp);// I don't know how to push_back here!!
return 0;

Dynamic 2d vector c++

vector<vector<double> > table;
for (int i = 0; i < 10; ++i)
Try the following

int n = 5;
std::vector< std::vector<int> > row(n);
for (int i=0; i<n; i++) {
   row[i].push_back( std::vector<int>(n) );

int n = 5;
std::vector< std::vector<int> > row(n, std::vector<int>( n ) );

Sort 2d vector c++

using namespace std;
vector< vector<int> > vect{{6, 6, 1, 4}, 
                               {7, 1, 6, 5}, 
                               {8, 3, 9, 2}};
for(auto x:vect){
        //For each element 'y' in 'x'
        for(auto y:x) cout << y << " ";
        cout << endl;
sort(vect[1].begin(), vect[1].end());
cout << "The 2D vector after sorting 2st row is:\n"; 
    for(auto x:vect){
        //For each element 'y' in 'x'
        for(auto y:x) cout << y << " ";
        cout << endl;
    6 6 1 4
    1 5 6 7
    8 3 9 2 

I showed you how to search a vector using primitive methods by primitive I mean by using our own function that we created it’s a bit harder to sort a vector using primitive methods.

Because it requires programming skills that you haven’t learned yet and aren’t appropriate for this course so for sorting a vector we’re going to stick with the built-in functions that are part of the C++ library.

So to do that we need to add that library into our code first thing so let’s do that now the library is called algorithm and it will give us the capabilities to call a sort function that will work with vectors.

You’ll notice that I’ve already put in the build vector and the display vector functions I want to modify build vector first.

So that we can specify the number of items we want to put in our vector so we’re going to add a parameter nums and then we’re going to change this hard-coded 1000 in the for loop.

So that it will reflect the number of items we actually want to build and then notice that I also have a display vector function for integers but I also need one for Strings because we’re going to do a sort on a string vector later.

So I need to copy this function and modify it to work with strings so I’m going to do that right now we’ll call it display vector s it’s got to have a new name.

And then we’re going to change the data type of the parameter to string that’s the only change we have to make to that function to make it work.

So let’s start out by building a numeric vector an integer vector and put some numbers in it and then see how it sorts so we’ll call the vector type with int and call it numbers then we’ll write build vector numbers.

And let’s do ten so that we can easily sort the vector and see the both the pre-sorted vector and the post sorted vector so we’ll do that by calling display vector first with the unsorted vector.

Let’s write a new line then we’ll call the sort function the sort function is called sort and it takes two arguments the beginning of the vector which we specify using a built-in function called begin.

And the ending of the vector which we specify using a built-in function called end those functions specify where the sort function should begin and end when it’s doing its sorting.

We do that because we could sort the first half or the second half or a middle slice but we want to sort the whole vector from beginning to end so we specify those two functions then let’s display the vector after it’s been sorted and put another new line that should be all we need.

So let’s build and run the program Oh semicolon but then in there now we’re ready to go so you see the vector unsorted starts at 211 and then has several other items through 954.

And then the vector sorted starts at 57 211 308 378 FC notice as we move down those items are in sorted order so the sort function works quite efficiently let’s try it with the larger size.

Let’s try it with a thousand items we won’t be able to see the unsorted vector when we display them and we’ll only see a bit or part of the sorted vector.

But just to see how quickly it does it there we go and we can see that the items are sorted let me pull it down a little bit where you can see it better and just for fun let’s try it with the hundred thousand items there’s the unsorted vector.

You can tell as we wade through it’s going to take quite a bit longer okay I just wanted to demonstrate this.

So you can see that it works with any size and displaying the vectors makes it take quite a bit longer and there we go into the display I’m probably going to cut this off before it gets finished because we’ve got quite a ways to go.

So I’m going to ctrl C here to stop it so that’s how the sort function works let’s try it with a string vector so it’s coming out our code to sort numbers and let’s create a new vector.

We’ll just do names again those are easy we’ll put some names into the vector names so excuse me for a second while I type you let’s do one more name let’s do something in the middle you must is mining.

Here we go let’s display the vector so we’ll call display vector s with names let’s put a new line after that then we’ll sort the vector using the very same function call very same arguments just a different vector name names instead of numbers then let’s display the vector again.

And put another new line that looks like all we need I think I’ve got all my semicolons in this time so let’s build and run the program and there you go.

So we had Raymond Cynthia David William Mike to start with and after the sort function is called Cynthia David Mike Raymond and William once again I encourage you to always use the built-in functions that are part of the C++ libraries.

They’re always tested well more efficient than what you can write yourself I do encourage you to learn more about sorting and in an advanced course you would learn some advanced sorting techniques but most professional programmers still believe that the built-in algorithms for searching and sorting are much more effective.

Resize 2d vector c++

for (int i = 0; i < n; ++i)
matrix.resize(COL, vector<char>(ROW));
Alternatively, when initializing or if you want to reset a non-empty vector, you can use the constructor overload taking a size and initial value to initialize all the inner vectors:

matrix = vector<vector<char> >(COL, vector<char>(ROW));

Passing 2d vector to function c++

void printMatrix(vector< vector<int> > *matrix)
specifies a pointer, it is essentially passed by reference. However, in C++, it's better to avoid pointers and pass a reference directly:

void printMatrix(vector< vector<int> > &matrix)


Push_back in 2d vector c++

push_back data in a 2d vector C++ of type int
std::vector<std::vector <int>> normal:

How to insert values in 2d vector c++

std::vector<std::vector<int>> grid(4);  // 4 rows

grid[3].resize(3)  // resize 4th row

grid[3][2] = 42;
Your 2D grid would then "look" like that:

 -                  <---  3 empty rows
 | 0 | 0 | 42 |

Get $100 to try DigitalOcean - Try for Free for 60 Days