#include <iostream> using namespace std; class Rocket{ public: //Rocket(string rocketclass); Rocket(short setstages); ~Rocket(); void setStages(short st){ stages = st; } short getStages(){ return stages; } void setThrust(int rpsPump); bool engineOnOff(bool sw); private: double thrust; bool switchEngine; short stages; }; Rocket::Rocket(short setstages) { stages=setstages; } Rocket::~Rocket() { //do nothing } class Engine{ public: Engine(short jetNr); private: string model; short jetnumber; double power; }; int main(){ short stages; Rocket SaturnV(1); SaturnV.setStages(2); cout<<"This Rocket consist now of "<<SaturnV.getStages()<<" Stages"<<endl; return 0; } /*int i=5; float f=10.2345678; double d=10.2345678; char c='G'; printf("Integer-Wert: %d \nFloat-Wert: %f \nDouble-Wert: %g \nCharacter: %c ",i,f,d,c); */
#include <iostream> using namespace std; void myFun() { cout << "myFun()\n"; } void CallBackFun(void (*fun_ptr)()) { cout << "CallBackFun()\n"; fun_ptr(); } int main() { void (*fun_ptr)() = myFun; fun_ptr(); CallBackFun(fun_ptr); return 0; }
#include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { //cout << "Hello World" << endl; // ifstream in; //in.open(argv[1], ios::binary); //while (getline(in, line)) { // points.insert(line); // } cout << "Hello World" << endl; string name = ""; char response = 'y'; // double xval; // double yval; while (response == 'y') { // cout << "Enter actor/actress name: " << "\n"; // getline(cin, name); // cout << "Enter x value: " << "\n"; // getline(cin, xval); // cout << "Enter y value: " << "\n"; // getline(cin, yval); // //Point onept = Point(xval, yval); // //points {3.2, 1.0} // KDT::iterator nearpt = tree.findNearestNeighbor(onept) // // Look for provided name in the BST // BST<string>::iterator item = tree.find(name); // if (nearpt != 0 && nearpt != tree.end()) { // cout << onept << " found!" << "\n"; // } els cout << "Search again? (y/n)" << "\n"; cin >> response; cin.ignore(); if (response != 'n' && response != 'y') { cout << "Invalid response, exiting..." << endl; break; } } return 0; }
#include<iostream> #include<vector> using namespace std; void printVector(vector<int> vec){ for(int i=0;i<vec.size();i++) cout<<vec.at(i)<<" "<<endl; } int main(){ vector<int> vec; vector<int>::iterator it; for(int i=0;i<10;i++){ vec.push_back(i+1); } printVector(vec); // iteration // it = vec.begin()+2; // cout<<"\n"<<*----it++<<endl; // pre & post processor // cout<<"\n"<<*it<<endl; // cout<<"\n"<<*it<<endl; //constant iteration // auto itc = vec.cbegin()+2; // constant iterator // // *itc = 20; // cant change because of const iterator // cout<<"\n"<<*itc++<<endl; // cout<<"\n"<<*itc<<endl; //reverse iteration // auto itr = vec.rbegin(); // auto itre = vec.rend(); // cout<<"\n"<<*itr++<<endl; // cout<<"\n"<<*itr<<endl; // cout<<"\n"<<*itre<<endl; // Capacity // cout<<"vec.size() = "<< vec.size()<<endl; // cout<<"vec.max_size() = "<< vec.max_size()<<endl; // cout<<"vec.capacity() = "<< vec.capacity()<<endl; // cout<<"vec.empty() = "<< vec.empty()<<endl; // vec.resize(10);// incase of decrasing size part data will loss // printVector(vec); // vec.resize(0); // cout<<"vec.empty() = "<< vec.empty()<<endl; // data access by at, [] , data(), back(),front() /* cout<<*vec.data()<<endl; cout<<*vec.begin()<<endl; auto i =vec.begin(); // addr of beging() and data() are same but having different data type int *j = vec.data(); printf("addr data() = %p\n",i); printf("addr begin() = %p\n",j); cout<<"at loc data() +3 = "<<*(i+3)<<endl; int element3rd = vec.at(2); int element3rd_ = vec[2]; int front = vec.front(); int back = vec.back(); cout<<"element3rd = "<<element3rd<<endl; cout<<"element3rd_ = "<<element3rd_<<endl; cout<<"vec.front = "<<front<<endl; cout<<"vec.back = "<<back<<endl; */ // modification cout<<"modification"<<endl; /* vec.push_back(21); vec.push_back(25); vec.push_back(44); printVector(vec); cout<<"---"<<endl; vec.pop_back(); vec.pop_back(); printVector(vec); // vec.assign(15,80); // data will loss and fill with 80 // vec.resize(15,80); // data preserve anly new element fill with 80 // printVector(vec); */ vector<int> vec2; vec2.push_back(44); vec2.push_back(45); vec2.push_back(46); vec2.push_back(47); cout<<"--"<<endl; vec.insert(vec.begin(),12); // insert single element vec.insert(vec.begin(),4,12); // same element n = 4 times vec.insert(vec.begin()+5,vec2.begin(),vec2.end());// inserting vector initializer_list<int> il = { 10, 20, 30 }; //initializer list; vec.insert(vec.begin()+5,il );// inserting vector printVector(vec); // erase by giving position or range of position using iterator cout<<"--erase--"<<endl; vec.erase(vec.begin());// single element vec.erase(vec.begin(),vec.begin()+10);//range from iterator printVector(vec); return 0; }
#include <iostream> #include <string> #include <map> #include <array> #include <vector> #include <sstream> #include <cstdint> template<typename T> struct Nullable { bool isNull; T value; Nullable<T>& operator=(const T& rhs) { this->value = rhs; return *this; } operator T() { return value; } Nullable<T>() : isNull(false) {} Nullable<T>(T value) : isNull(false), value(value) {} Nullable<T>(bool isNull, T value) : isNull(isNull), value(value) {} }; template<typename T> struct Nullable<std::vector<T>> { typedef std::vector<T> VT; bool isNull; VT value; Nullable<VT>& operator=(const VT& rhs) { this->value = rhs; return *this; } operator VT() { return value; } Nullable<VT>() : isNull(false) {} Nullable<VT>(VT value) : isNull(false), value(value) {} Nullable<VT>(bool isNull, VT value) : isNull(isNull), value(value) {} T& operator[](int index) { return value[index]; } }; template<typename T> bool operator== (const T &t, const Nullable<T> &n) { return !n.isNull && n.value == t; } template<typename T> bool operator== (const Nullable<T> &n, const T &t) { return !n.isNull && n.value == t; } void f1(int i) { std::cout << "f1: " << i << std::endl; } void f2(Nullable<int> n) { std::cout << "f2: " << n.value << std::endl; } struct Person { int age; }; bool operator ==(const Person& left, const Person& right) { return left.age == right.age; } int main(int argc, char* argv[]) { Person p{42}; Nullable<Person> np; np = p; Nullable<Person> np2 = p; Person p2{32}; Person p3 = np2; p2 = np2; if (p == np) { if (np == p) { std::cout << "both work" << std::endl; } else { std::cout << "one work" << std::endl; } } else { std::cout << "none work" << std::endl; } int i = 4; Nullable<int> ni; ni = i; Nullable<int> ni2 = i; int i2 = 1; i2 = ni2; f1(i2); f1(ni2); f2(i2); f2(ni2); std::vector<int> is = {1, 2, 3, 4}; Nullable<std::vector<int>> nis = is; std::cout << "first: " << (nis == is ? "equal" : "not equal") << std::endl; nis[2] = 6; std::cout << "second: " << (nis == is ? "equal" : "not equal") << std::endl; return 0; }
#include <iostream> #include <cstring> using namespace std; typedef int PostEvents; const string s[] { "eBegin", "", "eFile", "index.htm" }; void CallBack(int sock, PostEvents event, const char * pName, const char * pValue) { string e = s[event]+(pName != nullptr?pName:""); string v = s[event]+(pValue != nullptr?pValue:""); pName = e.c_str(); pValue = v.c_str(); // Scale.p_class(pName,pValue,sock); cout <<"Do: "<<pName<<" "<<pValue<<" "<<sock<< "\r\n"; } int main() { char Name[]="something"; char Value[] = "50"; int i = 1; cout << "Hello World" << endl; CallBack(2,i++,Name,Value); CallBack(2,i++,Name,Value); return 0; }
#include <iostream> using namespace std; struct Node { int val; Node *next; }; struct Node* head = NULL; void insert(int new_data) { struct Node* new_node = (struct Node*) malloc (sizeof(struct Node)); new_node->val = new_data; new_node->next = head; head = new_node; } void display() { struct Node* ptr; ptr = head; while (ptr != NULL) { cout<< ptr->val <<" "; ptr = ptr->next; } } int num_distinct(Node *lst) { bool is_dup; int ndistinct=0; struct Node *a; a = lst; while (lst != nullptr && lst->next != nullptr) { is_dup=false; if ( lst->val==lst->next->val){ is_dup=true; } if (!is_dup) ndistinct++; lst = lst->next; } return ndistinct; } int main() { insert(4); insert(4); insert(4); insert(4); insert(4); cout<<"The linked list is: "; display(); cout<<endl<<num_distinct(head)<<endl; //cout<<endl<<num_distinct(b,5)<<endl; return 0; }
#include <iostream> using namespace std; typedef int T[100]; int main() { T tt; cout<<sizeof(tt)<<endl; cout<<hash<int>()(123)<<endl; // cout<< hash<const char*>()("a") <<endl; hash<const char*> hashFunctor; cout<<hashFunctor("kiwi"); tuple<string, int, int> mytuple; cout<<mytuple.size()<<endl; return 0; }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more