Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.81 KB | None | 0 0
  1. Basic Data Types
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. int a;
  10. long b;
  11. char c;
  12. float d;
  13. double e;
  14. e;
  15. cin >>a>>b>>c>>d>>e;
  16. cout << fixed;
  17. cout << a<<'\n';
  18. cout << b<<'\n';
  19. cout << c<<'\n';
  20. cout << d<<'\n';
  21. cout << e<<'\n';
  22. return 0;
  23. }
  24.  
  25. Conditional Statements
  26.  
  27. #include <bits/stdc++.h>
  28.  
  29. using namespace std;
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35. string a[]={"Greater than 9","one","two","three","four","five","six","seven","eight","nine"};
  36.  
  37. int n;
  38. cin >> n;
  39. if(n>9){
  40. cout << a[0];
  41. }
  42. else {
  43. cout << a[n];
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. For Loop
  50.  
  51. #include <bits/stdc++.h>
  52.  
  53. using namespace std;
  54.  
  55. int main() {
  56. string a[] = {"one", "two", "three", "four", "five",
  57. "six", "seven", "eight", "nine"};
  58.  
  59. int n, c;
  60. cin >> n >> c;
  61. for (; n <= c; n++) {
  62. if (n > 9 && c > 9 && n % 2 == 0) {
  63. cout << "even" << endl;
  64. } else if (n > 9 && c > 9 && n % 2 != 0) {
  65. cout << "odd" << endl;
  66. } else {
  67. cout << a[n - 1] << endl;
  68. }
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74. Functions
  75.  
  76. #include <iostream>
  77. #include <cstdio>
  78. using namespace std;
  79.  
  80. int main (){
  81. int i;
  82. int a[4];
  83. for(i=0;i<4;i++){
  84. cin >> a[i];
  85. }
  86. int max=a[0];
  87. for(i=0;i<4;i++){
  88. if (a[i]>max){
  89. max=a[i];
  90. }
  91.  
  92. }
  93. cout<<max;
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. return 0; }
  101.  
  102. Pointer
  103.  
  104. # include <iostream>
  105. using namespace std;
  106. int main(){
  107. int a,b,c,d;
  108. cin >>a>>b;
  109. c=a+b;
  110. if (a>=b){
  111. d=a-b;}
  112. else{
  113. d=b-a;
  114. }
  115. cout<<c<<endl<<d;
  116. return 0;
  117. }
  118.  
  119. Arrays Introduction
  120.  
  121. #include <algorithm>
  122. #include <cmath>
  123. #include <cstdio>
  124. #include <iostream>
  125. #include <vector>
  126. using namespace std;
  127.  
  128. int main() {
  129. int i, n;
  130. cin >> n;
  131. int a[n];
  132. for (i = 0; i <= n - 1; i++) {
  133. cin >> a[i];
  134. }
  135. for (i = n - 1; i >= 0; i--) {
  136.  
  137. cout << a[i] << ' ';
  138. }
  139.  
  140. return 0;
  141. }
  142.  
  143. Variable Sized Arrays
  144.  
  145. #include <cmath>
  146. #include <cstdio>
  147. #include <vector>
  148. #include <iostream>
  149. #include <algorithm>
  150. using namespace std;
  151.  
  152.  
  153. int main() {
  154. int n,q;
  155. cin >> n >> q;
  156. vector< vector<int> > a(n);
  157.  
  158. // input each array
  159. for (int i=0;i<n;i++) {
  160. int k;
  161. cin >> k;
  162.  
  163. for (int j=0;j<k;j++) {
  164. int data;
  165. cin >> data;
  166. a[i].push_back(data);
  167. }
  168. }
  169.  
  170. // do the queries
  171. for (int i=0;i<q;i++) {
  172. int x,y;
  173. cin >> x >> y;
  174. cout << a[x][y] << endl;
  175. }
  176. return 0;
  177. }
  178.  
  179. StringStream
  180.  
  181. #include <iostream>
  182. #include <string>
  183.  
  184. using namespace std;
  185.  
  186. int main() {
  187. string s;
  188. cin >> s;
  189. for (size_t f = 0; (f = s.find(",", f)) != string::npos;
  190. s.replace(f, 1, "\n"))
  191. ;
  192. cout << s << endl;
  193. return 0;
  194. }
  195.  
  196. Strings
  197.  
  198. #include <bits/stdc++.h>
  199.  
  200. using namespace std;
  201.  
  202. int main() {
  203. string a, b;
  204. cin >> a >> b;
  205.  
  206. cout << a.length() << ' ' << b.length() << endl;
  207.  
  208. cout << a + b << endl;
  209.  
  210. swap(a[0], b[0]);
  211. cout << a << ' ' << b << endl;
  212.  
  213. return 0;
  214. }
  215.  
  216. Structs
  217.  
  218. #include <cmath>
  219. #include <cstdio>
  220. #include <vector>
  221. #include <iostream>
  222. #include <algorithm>
  223. using namespace std;
  224.  
  225. struct Student { int age; string first_name; string last_name; int standard; };
  226.  
  227. int main() { Student st; std::string str1; std::string str2; cin >> st.age; cin>>str1; cin>>str2; cin>> st.standard; st.first_name=str1.substr(0,50); st.last_name=str2.substr(0,50);
  228.  
  229. cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
  230.  
  231. return 0;
  232. }
  233.  
  234. Class
  235.  
  236. #include <iostream>
  237. #include <sstream>
  238. using namespace std;
  239.  
  240.  
  241.  
  242.  
  243. class Student {
  244. private:
  245. int age;
  246. string first_name;
  247. string last_name;
  248. int standard;
  249.  
  250. public:
  251.  
  252. void set_age (int a){
  253. age=a;
  254. }
  255. void set_first_name (string a){
  256. first_name=a;
  257. }
  258. void set_last_name (string b){
  259. last_name=b;
  260. }
  261. void set_standard (int c){
  262. standard=c;
  263. }
  264.  
  265. int get_age(){
  266. return age;
  267. }
  268. string get_first_name(){
  269. return first_name;
  270. }
  271. string get_last_name(){
  272. return last_name;
  273. }
  274. int get_standard(){
  275. return standard;
  276. }
  277.  
  278. string to_String(){
  279. return to_string(age) + "," + first_name + "," + last_name + "," +
  280. to_string(standard);
  281. }
  282.  
  283. };
  284.  
  285. int main() {
  286. int age, standard;
  287. string first_name, last_name;
  288.  
  289. cin >> age >> first_name >> last_name >> standard;
  290.  
  291. Student st;
  292. st.set_age(age);
  293. st.set_standard(standard);
  294. st.set_first_name(first_name);
  295. st.set_last_name(last_name);
  296.  
  297. cout << st.get_age() << "\n";
  298. cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
  299. cout << st.get_standard() << "\n";
  300. cout << "\n";
  301. cout << st.to_String();
  302.  
  303. return 0;
  304. }
  305.  
  306. Classes and Objects
  307.  
  308.  
  309. class Student{
  310. int scores[5];
  311. public:
  312. void input(){
  313. for(int i=0; i<5; i++){
  314. cin >> scores[i];
  315. }
  316. }
  317. int calculateTotalScore(){
  318. int total = 0;
  319. for(int i=0; i<5; i++){
  320. total += scores[i];
  321. }
  322. return total;
  323. }
  324. };
  325.  
  326.  
  327. Box It!
  328.  
  329. class Box{
  330. private:
  331. int l, b, h;
  332. public:
  333. Box(){
  334. l = 0;
  335. b = 0;
  336. h = 0;
  337. }
  338. Box(int length, int breadth, int height){
  339. l = length;
  340. b = breadth;
  341. h = height;
  342. }
  343. Box(const Box& B){
  344. l = B.l;
  345. b = B.b;
  346. h = B.h;
  347. }
  348.  
  349. int getLenght(){
  350. return l;
  351. }
  352. int getBreadth(){
  353. return b;
  354. }
  355. int getHeight(){
  356. return h;
  357. }
  358. long long CalculateVolume(){
  359. return (long long)l*b*h;
  360. }
  361.  
  362. friend bool operator < ( Box&A,Box& B){
  363. if( (A.l < B.l) || ((A.b < B.b) && (A.l == B.l)) || ((A.h < B.h) && (A.l == B.l) && (A.b == B.b)) ){
  364. return true;
  365. }else{
  366. return false;
  367. }
  368. };
  369.  
  370. friend ostream& operator<< (ostream& output, const Box& B){
  371. output << B.l << " " << B.b << " " << B.h;
  372. return output;
  373. }
  374. };
  375.  
  376. Vector-Sort
  377.  
  378. #include <vector>
  379. #include <iostream>
  380. #include <iterator>
  381. #include <algorithm>
  382.  
  383. int main() {
  384. std::vector<int> v;
  385. int n;
  386. std::cin >> n;
  387.  
  388. std::copy_n(std::istream_iterator<int>(std::cin), n, std::back_insert_iterator<std::vector<int>>(v));
  389. std::sort(v.begin(), v.end());
  390. std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  391.  
  392. return 0;
  393. }
  394.  
  395. Vector-Erase
  396.  
  397. #include <cmath>
  398. #include <cstdio>
  399. #include <vector>
  400. #include <iostream>
  401. #include <algorithm>
  402. using namespace std;
  403.  
  404.  
  405. int main() {
  406. int n;
  407. cin >> n;
  408. vector <int> v;
  409. int a;
  410. for(int i=0;i<n;i++){
  411. cin >> a;
  412. v.push_back(a);
  413. }
  414. int k;
  415. cin >> k;
  416. v.erase(v.begin()+k-1);
  417. int l,m;
  418. cin >> l >> m;
  419. v.erase(v.begin()+l-1,v.begin()+m-1);
  420. cout << v.size()<<endl;
  421. for(int i=0; i<v.size();i++){
  422. cout << v[i] << " ";
  423. }
  424. return 0;
  425. }
  426.  
  427. Lower Bound-STL
  428.  
  429. #include <cmath>
  430. #include <cstdio>
  431. #include <vector>
  432. #include <iostream>
  433. #include <algorithm>
  434. using namespace std;
  435.  
  436.  
  437. int main() {
  438. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  439. int m, num;
  440. cin >> m;
  441. vector<int> v;
  442. for (int i=0; i<m; i++){
  443. cin >> num;
  444. v.push_back(num);
  445. }
  446. int n, val;
  447. cin >> n;
  448. for (int i=0; i<n; i++){
  449. cin >> val;
  450. vector<int>::iterator low = lower_bound(v.begin(), v.end(), val);
  451. if (v[low - v.begin()] == val)
  452. cout << "Yes " << (low - v.begin()+1) << endl;
  453. else
  454. cout << "No " << (low - v.begin()+1) << endl;
  455. }
  456. return 0;
  457. }
  458.  
  459. Sets-STL
  460.  
  461. #include <cmath>
  462. #include <cstdio>
  463. #include <vector>
  464. #include <iostream>
  465. #include <set>
  466. #include <algorithm>
  467. using namespace std;
  468.  
  469.  
  470. int main() {
  471. int iCount;
  472. set<int> ss;
  473. cin >> iCount;
  474. for (int i=0; i<iCount; ++i){
  475. int type, query;
  476. cin >> type >> query;
  477. switch (type){
  478. case 1:
  479. ss.insert(query);
  480. break;
  481. case 2:
  482. ss.erase(query);
  483. break;
  484. case 3:
  485. cout << (ss.find(query) == ss.end() ? "No" : "Yes") << endl;
  486. break;
  487. }
  488. }
  489. return 0;
  490. }
  491.  
  492. Maps-STL
  493.  
  494. #include <cmath>
  495. #include <cstdio>
  496. #include <vector>
  497. #include <iostream>
  498. #include <set>
  499. #include <map>
  500. #include <algorithm>
  501. using namespace std;
  502.  
  503.  
  504. int main() {
  505. int q, type; cin >> q;
  506. map<string,int> clas; string name;
  507. for (int i(0), mark; i<q; ++i)
  508. {
  509. cin >> type >> name;
  510. if (type == 1){
  511. cin >> mark;
  512. clas[name] += mark;
  513. }
  514. else if (type == 2)
  515. clas.erase(name);
  516. else
  517. cout << clas[name] << "\n";
  518. }
  519. return 0;
  520. }
  521.  
  522. Print Pretty
  523.  
  524.  
  525.  
  526. cout << hex << showbase << nouppercase<<left;
  527. cout << (long long)A << endl;
  528.  
  529.  
  530. cout << setw(15) << showpos << setfill('_') << setprecision(2)<<fixed<<right;
  531. cout << B << endl;
  532.  
  533.  
  534. cout << scientific << setw(15) << setprecision(9) << uppercase<<noshowpos;
  535. cout << C << endl;
  536.  
  537. Inheritance Introduction
  538.  
  539. #include <cmath>
  540. #include <cstdio>
  541. #include <vector>
  542. #include <iostream>
  543. #include <algorithm>
  544. using namespace std;
  545.  
  546.  
  547. class Triangle{
  548. public:
  549. void triangle(){
  550. cout<<"I am a triangle\n";
  551. }
  552. };
  553. class Isosceles : public Triangle{
  554. public:
  555. void isosceles(){
  556. cout<<"I am an isosceles triangle\n";
  557. }
  558.  
  559. void description(){
  560. cout<<"In an isosceles triangle two sides are equal\n";
  561. }
  562. };
  563. int main(){
  564. Isosceles isc;
  565. isc.isosceles();
  566. isc.description();
  567. isc.triangle();
  568. return 0;
  569. }
  570.  
  571. Rectangle Area
  572.  
  573.  
  574. class Rectangle
  575. {
  576. public:
  577. Rectangle() = default;
  578. virtual void display() const { std::cout << _width << " " << _height << std::endl; }
  579. int area() const { return _width * _height; }
  580. protected:
  581. int _width = 0;
  582. int _height = 0;
  583. };
  584.  
  585. class RectangleArea : public Rectangle
  586. {
  587. public:
  588. RectangleArea() = default;
  589. virtual void display() const override { std::cout << area() << std::endl; }
  590. void read_input() { std::cin >> _width >> _height; }
  591. };
  592.  
  593. Multi Level Inheritance
  594.  
  595. #include <iostream>
  596. #include <memory>
  597.  
  598. class Shape
  599. {
  600. public:
  601. virtual const void triangle()const = 0;
  602. virtual ~Shape(){ }
  603. };
  604.  
  605. class Triangle: public Shape
  606. {
  607. public:
  608. virtual const void triangle()const override
  609. { std::cout<<"I am a triangle\n"; }
  610. virtual ~Triangle(){ }
  611. };
  612.  
  613. class Isosceles : public Triangle
  614. {
  615. public:
  616. virtual const void triangle()const override
  617. { std::cout<<"I am an isosceles triangle\n"; }
  618. virtual ~Isosceles(){ }
  619. };
  620. class Equilateral: public Isosceles
  621. {
  622. public:
  623. virtual const void triangle()const override
  624. { std::cout<<"I am an equilateral triangle\n"; }
  625. virtual ~Equilateral(){ }
  626. };
  627.  
  628. int main()
  629. {
  630. std::shared_ptr<Shape> obj[3];
  631.  
  632. obj[0] = std::make_shared<Equilateral>();
  633. obj[1] = std::make_shared<Isosceles>();
  634. obj[2] = std::make_shared<Triangle>();
  635.  
  636. for(auto it: obj)
  637. it->triangle();
  638. return 0;
  639. }
  640.  
  641.  
  642. C++ Class Templates
  643.  
  644.  
  645. template <class T> class AddElements {
  646. public:
  647. T element;
  648. AddElements(T i) {element = i;}
  649. T add(T i) {return element+i;}
  650. private:
  651.  
  652. };
  653. template <> class AddElements <string> {
  654. public:
  655. string element;
  656. AddElements(string i) {element = i;}
  657. string concatenate(string element2) {return element+element2;}
  658. private:
  659.  
  660. };
  661.  
  662. Overload Operators
  663.  
  664.  
  665. ostream &operator<<(ostream &os, const Complex &c) {
  666. return os << c.a << (c.b > 0 ? '+' : '-') << 'i' << c.b;
  667. }
  668.  
  669. Complex operator+(const Complex &a, const Complex &b) {
  670. return {a.a + b.a, a.b + b.b};
  671. }
  672.  
  673. //Overload operators + and << for the class complex
  674. //+ should add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)
  675. //<< should print a complex number in the format "a+ib"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement