Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.

Similar presentations


Presentation on theme: "STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints."— Presentation transcript:

1 STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints std::list second (4,100); // four ints with value 100 std::list third (second.begin(),second.end()); // iterating through second std::list fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::list fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are: "; for (std::list ::iterator it = fifth.begin(); it != fifth.end(); it++) std::cout << *it << ' '; std::cout << '\n'; return 0; }

2 Iterators: begin Return iterator to beginning end Return iterator to end rbegin Return reverse iterator to reverse beginning rend Return reverse iterator to reverse end cbegin Return const_iterator to beginning cend Return const_iterator to end crbegin Return const_reverse_iterator to reverse beginning crend Return const_reverse_iterator to reverse end Capacity: empty Test whether container is empty size Return size max_size Return maximum size Element access: front Access first element back Access last element Member Functions

3 Modifiers: assign assign Assign new content to container emplace_front Construct and insert element at beginning push_front Insert element at beginning pop_front Delete first element emplace_back Construct and insert element at the end push_back Add element at the end pop_back Delete last element emplace Construct and insert element insert Insert elements erase Erase elements swap Swap content resize Change size clear Clear content Member Functions

4 Operations: splice splice Transfer elements from list to list remove Remove elements with specific value remove_if Remove elements fulfilling condition unique Remove duplicate values merge Merge sorted lists sort Sort elements in container reverse Reverse the order of elements Member Functions

5 Example

6 File-Stream C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files. These classes are derived directly or indirectly from the classes istream, and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. Therefore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files.

7 // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); // or // ofstream myfile("example.txt"); myfile << "Writing this to a file.\n"; myfile << "Writing multiple lines." << endl; myfile.close(); return 0; } Example

8 Open and Close Open a file In order to open a file with a stream object we use its member function open(): open (filename, mode); filename : a null-terminated char sequence of type const char* representing the name of the file to be opened. mode : an optional parameter with a combination of the flags. ios::inOpen for input operations. ios::outOpen for output operations. ios::binaryOpen in binary mode. ios::ate Set the initial position at the end of the file. If this flag is not set to any value, the initial position is the beginning of the file. ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output- only operations. ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.

9 Example ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary); output operation at the end of the filein binary mode ofstream myfile1; ofstream myfile2; myfile1.open ("memo.txt"); myfile2.open ("messages.txt"); Multiple file-stream objects can be opened at the same time.

10 Open and Close Closing a file When we are finished with our input and output operations on a file we shall close it so that its resources become available again. In order to do that we have to call the stream's member function close(): myfile.close(); Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes. *By default, the file referred to is in the current directory – the directory from which you run the program. You can specify a complete path name ofstream fout("c:\\programs\\memo.txt");

11 Text Files Text file streams are those where we do not include the ios::binary flag in their opening mode. These files are designed to store text and thus all values that we input or output from/to them can suffer some formatting transformations, which do not necessarily correspond to their literal binary value. When a string is written to the console, each newline( \n ) in memory is translated into a carriage return-linefeed pair. \n -> \n\r \10 -> \10\13 Same translation must be done when writing to a file.

12 Text Files // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } good() becomes false if the end of the file has been reached or if some other error occurred.

13 More State Flag Functions bad() : Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left. fail() :Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number. eof() :Returns true if a file open for reading has reached the end. good() :It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true.

14 Binary Files fstream.read(addr, number_of_bytes);// read data into addr fstream.write(addr, number_of_bytes);// write data from addr The first argument( addr ) is a data address in memory and must have the type char*. The second argument( number_of_bytes ) specifies the size of the specific type. fstream binfil("somefile.bin"); int n = 1; double x = 215.3; char str[16] = "It's C++!"; binfil.write((char*)(&n), sizeof(n)); binfil.write((char*)(&x), sizeof(x)); binfil.write(str, sizeof(str));

15 File Position Pointers Both istream and ostream provide member functions for repositioning the file- position pointer. These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream. The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream. The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file's starting location.

16 Example // position to the nth byte of fileObject (assumes ios::beg) fileObject.seekg( n ); // position n bytes forward in fileObject fileObject.seekg( n, ios::cur ); // position n bytes back from end of fileObject fileObject.seekg( n, ios::end ); // position at end of fileObject fileObject.seekg( 0, ios::end );


Download ppt "STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints."

Similar presentations


Ads by Google