1 / 10

C++ STL and Example Things to Start Doing

Get started with C++ (Xcode/Visual Studio/gcc) by making a helloworld.cpp program. Learn about C++ libraries for I/O, including <iostream> and filestreams. Explore the C++ STL, which includes containers, iterators, algorithms, functions, and more. Discover how to use dictionaries or associative arrays with <map> and <unordered_map>. Gain an understanding of iterators and pairs in the STL. Finally, learn how to work with filestreams in C++ to read from and write to files.

ctalbott
Download Presentation

C++ STL and Example Things to Start Doing

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. STL and Example

  2. Things start doing • Get C++ (Xcode/Visual Studio/gcc) • Make a helloworld.cpp program and build it.

  3. C++ Libraries – I/O • <iostream> • cout << "thing to print"; • Prints all the fundamental types • Prints string variables • Can be extended to print other types • cin >> variable; • Reads input from standard input • Parses all fundamental types • Spaces separate different inputs • Will read words into string variables • White space separates inputs and input is read with enter

  4. C++ STL • The Standard Template Library • C++ library parameterized by types similar to Java generics • Containers • Iterators • Algorithms • Functions

  5. Review <vector> • Declaration: std::vector<T>vec= {initializer list} • Access and modify: vec[index] = value • Assignment of whole array: new_vec = vec • Find size: vec.size() • Extend: vec.push_back(val)

  6. C++ Libraries – Dictionary or Associative Array • Take two types first key and second is value. <key,value> • Use as an array indexed by key values. • <map> • Whole container can be accessed in key order • O(log n) for most operations • <unordered_map> • Accessing the whole container is possible but the order is not defined • O(1) expected for most operations

  7. <map> • Declaration: std::map<T1,T2>my_map • Access and modify: my_map[index] = value • Assignment of whole array: new_map = my_map • Find size: my_map.size() • Find: my_map.find(key) • Returns an iterator to a pair <key,value>

  8. STL iterators and pair • Iterators • Acts like a pointer access the value with * or -> as with a pointer • Used to interact with STL containers • STL pair • Two types <T1,T2> • Access through public member variables firstand second

  9. Filestreams • ofstream– Write to a file • ifstream– Read from a file • fstream– Read and write from a file • Open the file fstream.open(filename) • Close the file fstream.close()

  10. Gradebook

More Related