1 / 23

CONTAINERS: c-STYLE ARRAY

CONTAINERS: c-STYLE ARRAY. // arrays example #include < iostream > using namespace std ; int foo [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; ++n ) { result += foo[n]; } cout << result; }. C-STYLE MULTI-DIMENSIONAL ARRAY. ARRAY.

lesterhays
Download Presentation

CONTAINERS: c-STYLE ARRAY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CONTAINERS: c-STYLE ARRAY // arrays example #include <iostream> using namespace std; int foo [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; ++n ) { result += foo[n]; } cout << result; }

  2. C-STYLE MULTI-DIMENSIONAL ARRAY

  3. ARRAY #include <iostream> using namespace std; // arrays as parameters void printarray (intarg[], intlength) { for (int n=0; n<length; ++n) cout << arg[n] << ' '; cout << '\n'; } int main () { intfirstarray[] = {5, 10, 15}; intsecondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray, 3); // Note: arrays passed by reference! printarray (secondarray, 5); }

  4. ARRAY (not much used!)

  5. C++ ARRAY (not much used!) #include <iostream> #include <array> using namespace std; int main() { array<int,3> myarray {10,20,30}; for (int i=0; i<myarray.size(); ++i) ++myarray[i]; for (int elem : myarray) cout << elem << '\n'; } #include <iostream> #include <array> using namespace std; int main() { array<double,3> myarray {10.0,20.1,30.2}; for (int i=0; i<myarray.size(); ++i) ++myarray[i]; for (double elem : myarray) cout << elem << '\n'; }

  6. CONTAINERS: c-STRING You may declare like: char myword[] = { 'B', 'y', 'e', '\0' }; // really needs an extra character to terminate char myword[] = "Bye"; However, you may NOT reassign like: myword = "ByeBye"; myword[] = "ByeBye";

  7. C++ String #include <iostream> #include <string> using namespace std; int main () { char question1[] = "What is your name? "; // c-string string question2 = "Where do you live? "; // c++ string char answer1 [80]; string answer2; cout << question1; cin >> answer1; cout << question2; cin >> answer2; cout << "Hello, " << answer1; cout << " from " << answer2 << "!\n"; }

  8. String #include <iostream> #include <string> using namespace std; int main () { char question1[] = "What is your name? "; string question2 = "Where do you live? "; char answer1 [80]; string answer2; cout << question1; cin >> answer1; cout << question2; cin >> answer2; cout << "Hello, " << answer1; cout << " from " << answer2 << "!\n"; cout << answer2.size(); // try answer1.size() }

  9. CONTAINER: VECTOR #include <iostream> #include <vector> using namespace std; int main() { vector<double> student_marks(2); // Note: a Template class for (vector<double>::size_type i = 0; i < 2; i++) { cout << "Enter marks for student #" << i+1 << ": "; cin >> student_marks[i]; } // ... Do some stuff with the values } https://cal-linux.com/tutorials/vectors.html

  10. Vector #include <iostream> #include <vector> using namespace std; int main() { vector<double> student_marks; // no size specified here, vector contains // no element within it currently int num_students; cout << "Number of students: "; cin >> num_students; student_marks.resize(num_students); for (vector<double>::size_type i = 0; i < num_students; i++) { cout << "Enter grades for student #" << i+1 << ": "; cin >> student_marks[i]; } } https://cal-linux.com/tutorials/vectors.html

  11. Vector #include <iostream> #include <vector> using namespace std; int main() {    vector<double> student_marks;    double mark;    char answer;    cout << "Enter marks (y/n)? " << flush;    cin >> answer;    while (answer == 'y' || answer == 'Y') {        cout << "Enter value: " << flush;        cin >> mark;        student_marks.push_back (mark); // typical way of entering data in a vector         cout << "More students (y/n)? " << flush;        cin >> answer;    }    return 0;} https://cal-linux.com/tutorials/vectors.html

  12. Vectors can grow or shrink int main() { vector<double> values(10); // Create a vector with 10 elements (initialized to 0.0) values.insert (values.begin() + 5, 1.4142); // Insert 1.4142 at position 5 (before the element that was at position 5) // Shifts all data after inserted position values.remove (values.begin() + 3); // Remove element at position 3, will shrink automatically https://cal-linux.com/tutorials/vectors.html

  13. Vector #include <iostream> #include <vector> using namespace std; int main() { vector<double> student_marks; int num_students; cout << "Number of students: "; cin >> num_students; student_marks.resize (num_students); for (vector<double>::size_type i = 0; i < num_students; i++) { cout << "Enter marks for student #" << i+1 << ": "; cin >> student_marks[i]; } for (vector<double>::iterator i = student_marks.begin(); i != student_marks.end(); ++i) { cout << *i << endl; // iterator is really a pointer to an element of a vector } } https://cal-linux.com/tutorials/vectors.html

  14. Stack // stack::push/pop #include <iostream> // for std::cout #include <stack> // for std::stack // I deliberately left “using …” here int main () { std::stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); std::cout << "Popping out elements..."; while (!mystack.empty()) { std::cout << ' ' << mystack.top(); mystack.pop(); } std::cout << '\n'; } Popping out elements... 4 3 2 1 0 http://www.cplusplus.com/reference/stack/stack/push/

  15. Queue // queue::push/pop #include <iostream> // std::cin, std::cout #include <queue> // std::queue int main () { std::queue<int> myqueue; int myint; std::cout << "Please enter some integers (enter 0 to end):\n"; do { std::cin >> myint; myqueue.push (myint); } while (myint); std::cout << "myqueue contains: "; while (!myqueue.empty()) { std::cout << ' ' << myqueue.front(); myqueue.pop(); } std::cout << '\n'; } Please enter some integers (enter 0 to end): 5 4 6 0 myqueue contains: 5 4 6 0 http://www.cplusplus.com/reference/queue/queue/push/

  16. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { std::cout << ' ' << mypq.top(); mypq.pop(); } cout << '\n'; } 30 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  17. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << ' ' << mypq.top(); mypq.pop(); } cout << '\n'; } 100 | 30 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  18. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << ' ' << mypq.top(); mypq.pop(); } cout << '\n'; } 100 | | 30 25 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  19. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { std::priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); std::cout << "Popping out elements..."; while (!mypq.empty()) { std::cout << ' ' << mypq.top(); mypq.pop(); } std::cout << '\n'; } • 100 • | | • 25 • | • 40 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  20. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << ' ' << mypq.top(); mypq.pop(); } std::cout << '\n'; } • 100 • | | • 25 • | • 40 • 40 • | | • 25 • 40 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  21. Priority_queue // priority_queue::push/pop #include <iostream> // std::cout #include <queue> // std::priority_queue using namespace std int main () { priority_queue<int> mypq; // default: high to low mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << ' ' << mypq.top(); mypq.pop(); } cout << '\n'; } Popping out elements... 100 40 30 25 http://www.cplusplus.com/reference/queue/priority_queue/pop/

  22. Priority_queue is for max-heap by default, Work around to use it for min-heap: // priority_queue::top #include <iostream> #include <vector> #include <queue> // std::priority_queue using namespace std; int main () { // Creates a min heap priority_queue <int, vector<int>, greater<int> > pq; pq.push(5); pq.push(1); pq.push(10); pq.push(30); pq.push(20); // One by one extract items from min heap while (pq.empty() == false) { cout << pq.top() << " "; pq.pop(); } } 1 5 10 20 30

  23. List of all containers in Standard Template Library (STL) Under namespace std: http://www.cplusplus.com/reference/stl/

More Related