Lecture3 - CS213 C Programming Lecture#3 Library Types...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CS213: C++ Programming Lecture #3: Library Types Chapter 3
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What We Will Cover Today Agenda: - namespaces - string type - vector type - iterators - bitset type
Image of page 2
Intro to Namespaces So far, our example programs have looked something like this: #include <iostream> int main(int argc, char *argv[]) { std::cout << “Hello World!” << std::endl; return 0; } What does the std:: mean? In this case, it refers to a namespace .
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Intro to Namespaces A namespace is similar to a package in Java. It provides an additional “name” to any symbol to help keep it unique. All standard C++ library functions are in a namespace called std . To use them, we must prefix the function name with std:: Hence when we see std::cout, it really means “call the function cout() defined in the std namespace.”
Image of page 4
Intro to Namespaces However, this can be cumbersome. Prefixing each function call with std:: can clutter up code. A solution is to utilize some additional syntax, like so: #include <iostream> using std::cout; using std::endl; int main(int argc, char *argv[]) { cout << “Hello World!” << endl; }
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Intro to Namespaces In utilizing the using directive, we can specify which functions in the std namespace we want to use in this file Then, all we have to do in our code is call the function in the namespace by its simple name-- without including the namespace information. This allows for cleaner, simpler code. There is another form of syntax which helps us achieve the same goal. Consider the following...
Image of page 6
Intro to Namespaces A different way... #include <iostream> using namespace std; int main(int argc, char *argv[]) { cout << “Hello World!” << endl; return 0; } This different form of using tells the compiler that any undefined symbol should be looked for in the std namespace before generating a “symbol not defined” type error.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Intro to Namespaces In fact, many default C++ projects generated by
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern