fork(59) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node {
  5. string str;
  6. int num;
  7. double doub;
  8. char x;
  9.  
  10. node(str_, num_, doub_, x_) {
  11. str = str_;
  12. num = num_;
  13. doub = doub_;
  14. x = x_;
  15. }
  16. };
  17. array<int, 3> arr; // -> {0, 0, 0}
  18.  
  19. // max size of 10^7 -> int, double, char
  20. int arr[10000000];
  21.  
  22. // max size of 10^8 -> bool
  23. bool arr[100000000];
  24. int main() {
  25.  
  26. // max size of 10^6 -> int, double, char
  27. int arr[1000000];
  28.  
  29.  
  30. // max size of 10^7 -> bool
  31. bool arr[10000000];
  32.  
  33. double val = 10.0;
  34. cout << val << endl; // prints 10.0
  35.  
  36. cout << raj::getVal() << endl; // prints 50
  37.  
  38.  
  39. int
  40. double
  41. char
  42.  
  43.  
  44. // create a data type where you store
  45. {string, int, double, char}
  46.  
  47.  
  48. // wrong way of defining
  49. node raj;
  50. raj.str = "striver";
  51. raj.num = 79;
  52. raj.doub = 91.0;
  53.  
  54. node raj = new node("striver", 79, 91.0, "");
  55.  
  56.  
  57. {arr[], int, double};
  58.  
  59.  
  60.  
  61. // Arrays -> int arr[100];
  62.  
  63.  
  64. array<int, 3> arr; // -> {?, ?, ?}
  65.  
  66.  
  67. array<int, 5> arr = {1}; // -> {1, 0, 0, 0, 0}
  68.  
  69.  
  70. int arr[10000] = {0};
  71.  
  72.  
  73.  
  74. array<int, 5> arr;
  75. arr.fill(10); -> /// {10, 10, 10, 10, 10}
  76.  
  77.  
  78. arr.at(index);
  79.  
  80. for(int i = 0;i<5;i++) {
  81. cout << arr.at(i) << " ";
  82. }
  83.  
  84.  
  85. // iterators
  86. // begin(), end(), rbegin(), rend()
  87.  
  88. //
  89.  
  90. array<int, 5> arr = {1, 3, 4, 5, 6};
  91. for(auto it: arr.begin(); it!=arr.end();it++) {
  92. cout << *it << " ";
  93. }
  94.  
  95.  
  96.  
  97. for(auto it: arr.rbegin(); it>arr.rend();it++) {
  98. cout << *it << " ";
  99. }
  100.  
  101. for(auto it: arr.end() - 1; it>=arr.begin();it--) {
  102. cout << *it << " ";
  103. }
  104.  
  105. // for each loop
  106. for(auto it: arr) {
  107. cout << it << " ";
  108. }
  109.  
  110. string s = "xhegcwe";
  111. // x h e g c w e
  112. for(auto c:s) {
  113. cout << c << " ";
  114. }
  115.  
  116.  
  117. // size
  118. cout << arr.size();
  119.  
  120. // front
  121. cout << arr.front(); // arr.at(0);
  122.  
  123. // back
  124. cout << arr.back(); // arr.at(arr.size() - 1);
  125.  
  126.  
  127.  
  128.  
  129. // VECTOR
  130.  
  131. int arr[50];
  132.  
  133. // segmentation fault if you push_back 10^7 times
  134.  
  135. vector<int> arr; // -> {}
  136. cout << arr.size() << endl; // -> print 0
  137. arr.push_back(0); // {0}
  138. arr.push_back(2); // {0,2}
  139. cout << arr.size() << endl; // -> print 2
  140. arr.pop_back(); // {0}
  141. cout << arr.size() << endl; // print 1
  142.  
  143. arr.push_back(0); // {0,0}
  144. arr.push_back(2); // {0,0,2}
  145.  
  146.  
  147. vec.clear(); // --> erase all elements at once {}
  148.  
  149.  
  150. vector<int> vec1(4, 0); // -> {0,0,0,0}
  151. vector<int> vec2(4, 10); // -> {10,10,10,10}
  152.  
  153. // copy the entire vec2 into vec3
  154. vector<int> vec3(vec2.begin(), vec2.end()); // -> [)
  155. vector<int> vec3(vec2);
  156.  
  157.  
  158.  
  159. vector<int> raj;
  160. raj.push_back(1); // raj.emplace_back(1); // emplace_back takes lesser time than push back
  161. raj.push_back(3);
  162. raj.push_back(2);
  163. raj.push_back(5); // -> {1, 3, 2, 5}
  164.  
  165. vector<int> raj1(raj.begin(), raj.begin() + 2); // -> {1, 3}
  166.  
  167.  
  168. // lower bound , upper bound
  169.  
  170. // swap swap(v1, v2)
  171. // begin(), end(), rbegin(), rend()
  172.  
  173.  
  174. // to defining 2d vectors
  175.  
  176. vector<vector<int>> vec;
  177.  
  178. vector<int> raj1;
  179. raj1.push_back(1);
  180. raj1.push_back(2);
  181.  
  182. vector<int> raj2;
  183. raj2.push_back(10);
  184. raj2.push_back(20);
  185.  
  186. vector<int> raj3;
  187. raj3.push_back(19);
  188. raj3.push_back(24);
  189. raj3.push_back(27);
  190.  
  191. vec.push_back(raj1);
  192. vec.push_back(raj2);
  193. vec.push_back(raj3);
  194.  
  195. // it is vector itself
  196. for(auto vctr: vec) {
  197. for(auto it: vctr) {
  198. cout << it << " ";
  199. }
  200. cout << endl;
  201. }
  202.  
  203.  
  204. for(int i = 0;i<vec.size();i++) {
  205. for(int j = 0;j<vec[i].size();j++) {
  206. cout << vec[i][j] << " ";
  207. }
  208. cout << endl;
  209. }
  210.  
  211.  
  212. // define 10 x 20
  213. vector<vector<int>> vec(10, vector<int> (20, 0));
  214. vec.push_back(vector<int>(20, 0));
  215. cout << vec.size() << endl; // 11 prints
  216.  
  217. vec[2].push_back(1);
  218.  
  219.  
  220. vector<int> arr[4];
  221. arr[1].push_back(0);
  222.  
  223.  
  224. // 10 x 20 x 30 // int arr[10][20][30]
  225. vector<vector<vector<int>>> vec(10, vector<vector<int>> vec(20, vector<int> (30, 0));)
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:10:11: error: expected ‘)’ before ‘,’ token
  node(str_, num_, doub_, x_) {
      ~    ^
           )
prog.cpp:20:17: error: conflicting declaration ‘int arr [10000000]’
 int arr[10000000];
                 ^
prog.cpp:17:15: note: previous declaration as ‘std::array<int, 3> arr’
 array<int, 3> arr; // -> {0, 0, 0}
               ^~~
prog.cpp:23:20: error: conflicting declaration ‘bool arr [100000000]’
  bool arr[100000000];
                    ^
prog.cpp:17:15: note: previous declaration as ‘std::array<int, 3> arr’
 array<int, 3> arr; // -> {0, 0, 0}
               ^~~
prog.cpp: In function ‘int main()’:
prog.cpp:31:19: error: conflicting declaration ‘bool arr [10000000]’
  bool arr[10000000];
                   ^
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:36:10: error: ‘raj’ has not been declared
  cout << raj::getVal() << endl; // prints 50
          ^~~
prog.cpp:39:2: error: expected primary-expression before ‘int’
  int
  ^~~
prog.cpp:54:7: error: redeclaration of ‘node raj’
  node raj = new node("striver", 79, 91.0, "");
       ^~~
prog.cpp:49:7: note: ‘node raj’ previously declared here
  node raj;
       ^~~
prog.cpp:54:45: error: no matching function for call to ‘node::node(const char [8], int, double, const char [1])’
  node raj = new node("striver", 79, 91.0, "");
                                             ^
prog.cpp:4:8: note: candidate: ‘node::node()’
 struct node {
        ^~~~
prog.cpp:4:8: note:   candidate expects 0 arguments, 4 provided
prog.cpp:4:8: note: candidate: ‘node::node(const node&)’
prog.cpp:4:8: note:   candidate expects 1 argument, 4 provided
prog.cpp:4:8: note: candidate: ‘node::node(node&&)’
prog.cpp:4:8: note:   candidate expects 1 argument, 4 provided
prog.cpp:57:7: error: expected primary-expression before ‘]’ token
  {arr[], int, double};
       ^
prog.cpp:57:10: error: expected primary-expression before ‘int’
  {arr[], int, double};
          ^~~
prog.cpp:64:16: error: conflicting declaration ‘std::array<int, 3> arr’
  array<int, 3> arr;  // -> {?, ?, ?}
                ^~~
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:67:16: error: conflicting declaration ‘std::array<int, 5> arr’
  array<int, 5> arr = {1}; // -> {1, 0, 0, 0, 0}
                ^~~
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:70:15: error: conflicting declaration ‘int arr [10000]’
  int arr[10000] = {0};
               ^
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:74:16: error: conflicting declaration ‘std::array<int, 5> arr’
  array<int, 5> arr;
                ^~~
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:75:6: error: request for member ‘fill’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.fill(10); -> /// {10, 10, 10, 10, 10}
      ^~~~
prog.cpp:75:16: error: expected primary-expression before ‘->’ token
  arr.fill(10); -> /// {10, 10, 10, 10, 10}
                ^~
prog.cpp:81:15: error: request for member ‘at’ in ‘arr’, which is of non-class type ‘int [1000000]’
   cout << arr.at(i) << " ";
               ^~
prog.cpp:90:16: error: conflicting declaration ‘std::array<int, 5> arr’
  array<int, 5> arr = {1, 3, 4, 5, 6};
                ^~~
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:91:19: error: request for member ‘begin’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.begin(); it!=arr.end();it++) {
                   ^~~~~
prog.cpp:91:26: error: expected ‘)’ before ‘;’ token
  for(auto it: arr.begin(); it!=arr.end();it++) {
     ~                    ^
                          )
prog.cpp:91:28: error: ‘it’ was not declared in this scope
  for(auto it: arr.begin(); it!=arr.end();it++) {
                            ^~
prog.cpp:91:28: note: suggested alternative: ‘int’
  for(auto it: arr.begin(); it!=arr.end();it++) {
                            ^~
                            int
prog.cpp:91:36: error: request for member ‘end’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.begin(); it!=arr.end();it++) {
                                    ^~~
prog.cpp:97:19: error: request for member ‘rbegin’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.rbegin(); it>arr.rend();it++) {
                   ^~~~~~
prog.cpp:97:27: error: expected ‘)’ before ‘;’ token
  for(auto it: arr.rbegin(); it>arr.rend();it++) {
     ~                     ^
                           )
prog.cpp:97:36: error: request for member ‘rend’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.rbegin(); it>arr.rend();it++) {
                                    ^~~~
prog.cpp:101:19: error: request for member ‘end’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.end() - 1; it>=arr.begin();it--) {
                   ^~~
prog.cpp:101:28: error: expected ‘)’ before ‘;’ token
  for(auto it: arr.end() - 1; it>=arr.begin();it--) {
     ~                      ^
                            )
prog.cpp:101:38: error: request for member ‘begin’ in ‘arr’, which is of non-class type ‘int [1000000]’
  for(auto it: arr.end() - 1; it>=arr.begin();it--) {
                                      ^~~~~
prog.cpp:118:14: error: request for member ‘size’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.size();
              ^~~~
prog.cpp:121:14: error: request for member ‘front’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.front(); // arr.at(0);
              ^~~~~
prog.cpp:124:14: error: request for member ‘back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.back(); // arr.at(arr.size() - 1);
              ^~~~
prog.cpp:131:12: error: conflicting declaration ‘int arr [50]’
  int arr[50];
            ^
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:135:14: error: conflicting declaration ‘std::vector<int> arr’
  vector<int> arr; // -> {}
              ^~~
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:136:14: error: request for member ‘size’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.size() << endl; // -> print 0
              ^~~~
prog.cpp:137:6: error: request for member ‘push_back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.push_back(0); // {0}
      ^~~~~~~~~
prog.cpp:138:6: error: request for member ‘push_back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.push_back(2); // {0,2}
      ^~~~~~~~~
prog.cpp:139:14: error: request for member ‘size’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.size() << endl; // -> print 2
              ^~~~
prog.cpp:140:6: error: request for member ‘pop_back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.pop_back(); // {0}
      ^~~~~~~~
prog.cpp:141:14: error: request for member ‘size’ in ‘arr’, which is of non-class type ‘int [1000000]’
  cout << arr.size() << endl; // print 1
              ^~~~
prog.cpp:143:6: error: request for member ‘push_back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.push_back(0); // {0,0}
      ^~~~~~~~~
prog.cpp:144:6: error: request for member ‘push_back’ in ‘arr’, which is of non-class type ‘int [1000000]’
  arr.push_back(2); // {0,0,2}
      ^~~~~~~~~
prog.cpp:147:2: error: ‘vec’ was not declared in this scope
  vec.clear(); // --> erase all elements at once {}
  ^~~
prog.cpp:155:23: error: redeclaration of ‘std::vector<int> vec3’
  vector<int> vec3(vec2);
                       ^
prog.cpp:154:14: note: ‘std::vector<int> vec3’ previously declared here
  vector<int> vec3(vec2.begin(), vec2.end()); // -> [)
              ^~~~
prog.cpp:159:14: error: conflicting declaration ‘std::vector<int> raj’
  vector<int> raj;
              ^~~
prog.cpp:49:7: note: previous declaration as ‘node raj’
  node raj;
       ^~~
prog.cpp:160:6: error: ‘struct node’ has no member named ‘push_back’
  raj.push_back(1); // raj.emplace_back(1); // emplace_back takes lesser time than push back
      ^~~~~~~~~
prog.cpp:161:6: error: ‘struct node’ has no member named ‘push_back’
  raj.push_back(3);
      ^~~~~~~~~
prog.cpp:162:6: error: ‘struct node’ has no member named ‘push_back’
  raj.push_back(2);
      ^~~~~~~~~
prog.cpp:163:6: error: ‘struct node’ has no member named ‘push_back’
  raj.push_back(5); // -> {1, 3, 2, 5}
      ^~~~~~~~~
prog.cpp:165:23: error: ‘struct node’ has no member named ‘begin’
  vector<int> raj1(raj.begin(), raj.begin() + 2);  // -> {1, 3}
                       ^~~~~
prog.cpp:165:36: error: ‘struct node’ has no member named ‘begin’
  vector<int> raj1(raj.begin(), raj.begin() + 2);  // -> {1, 3}
                                    ^~~~~
prog.cpp:178:14: error: redeclaration of ‘std::vector<int> raj1’
  vector<int> raj1;
              ^~~~
prog.cpp:165:14: note: ‘std::vector<int> raj1’ previously declared here
  vector<int> raj1(raj.begin(), raj.begin() + 2);  // -> {1, 3}
              ^~~~
prog.cpp:196:17: error: unable to deduce ‘auto&&’ from ‘vec’
  for(auto vctr: vec) {
                 ^~~
prog.cpp:197:16: error: unable to deduce ‘auto&&’ from ‘vctr’
   for(auto it: vctr) {
                ^~~~
prog.cpp:220:19: error: conflicting declaration ‘std::vector<int> arr [4]’
  vector<int> arr[4];
                   ^
prog.cpp:27:6: note: previous declaration as ‘int arr [1000000]’
  int arr[1000000];
      ^~~
prog.cpp:221:9: error: request for member ‘push_back’ in ‘arr[1]’, which is of non-class type ‘int’
  arr[1].push_back(0);
         ^~~~~~~~~
prog.cpp:225:58: error: expected primary-expression before ‘vec’
  vector<vector<vector<int>>> vec(10, vector<vector<int>> vec(20, vector<int> (30, 0));)
                                                          ^~~
prog.cpp:225:87: error: expected primary-expression before ‘)’ token
  vector<vector<vector<int>>> vec(10, vector<vector<int>> vec(20, vector<int> (30, 0));)
                                                                                       ^
stdout
Standard output is empty