c++ smart pointer 2d array
C++
#include <iostream>
#include <memory>
using namespace std;
int main(){
//this just builds a 2D array pi that looks like
//0 1 2 3 4
//5 6 7 8 9
//10 11 12 13
//... 24
int** pi=new int*[5];
//this counter is augmented by 5 in every loop,
//for the value to be 0...,5,..10etc
int counter=0;
for(int j=0;j<5;j++){
int* i=new int[5];
for(int j=0;j<5;j++)
i[j]=j+counter;
counter=counter+5;
pi[j]=i;
//just to print out the array
for(int k=0;k<5;k++)
cout<<pi[j][k]<<" ";
cout<<endl;
}
cout<<endl;
//trying the same thing using smart pointers
unique_ptr<int*[]> smp_pi(new int*[5]);
counter=0;
for(int j=0;j<5;j++){
unique_ptr<int[]> smp_i(new int[5]);
for(int k=0;k<5;k++){
smp_i[k]=counter+k;
cout<<smp_i[k]<<" ";
}
counter=counter+5;
cout<<endl;
smp_pi[j]=&smp_i[0];
//smp_pi[j]=smp_i; //this does not compile. why?
}
cout<<endl;
for(int j=0;j<5;j++){
for(int k=0;k<5;k++)
cout<<smp_pi[j][k]<<" ";
cout<<endl;
}
return 0;
}
Also in C++:
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- random number in c++
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- Find the duplicate in an array of N integers.
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- ceil in c++
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- sfml base program
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- -> cpp
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- vector in c++ class
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++