Вы находитесь на странице: 1из 14

Files: File is a collection of related data stored in a particular area on the disk.

File input & output streams : File uses streams as an interface between the program and file. Opening and closing a file: Each file contains File name Data type Purpose - input or output Opening method 2 methods are used 1. using constructor only one file in stream 2. using open member function multiple files using one stream. Open a file using constructor: Ofstream outfile(result); open a file named result for output. Ifstream infile(data); open a file named data for input only. Opening file using open function : File-stream-class stream-obj; Stream-obj.open(file name); Ex Ofstream outfile; Outfile.open(data); Outfile.close();

Write a program to read a text file and copy it into another file #include<iostream> #include<fstream> #include<string> Void main() { char str[80] ; Cout<<enter a string; Cin>>str; Int len=strlen(str); fstream file1; file.open(text , ios:out); for(int i=0; i< len; i++) file1.put(str[i]); file1.close(); file1.open(text , ios::in ); fstream file2; file2.open(sample,ios:out); while(file1) { file1.get(ch); File2.put(ch); } }

Write a program to read a text file and count number of character in it #include<iostream> #include<fstream> #include<string> Void main() { char str[80] ; int count =0; Cout<<enter a string; Cin>>str; Int len=strlen(str); fstream file; file.open(text , ios::in | ios:out); for(int i=0; i< len; i++) file.put(str[i]); File.seekg(0); Char ch; (while(fie) { file.get(ch); Count++; } Cout<number of char in the file = <<count; }

STL standard template library: The general purpose templatized classes (data st) and functions (algo) that could be used as a standard approach for storing and processing of data. collection of these generic classes and functions is called STL. STL save time and effort by reusing the well written and well tested components. STL components are part of standard C++ library and defined in namespace std as follows using namespace std; Components of STL 1.containers 2. algorithms 3.iterators STL algorithms Retrieving or non-mutating algorithms Mutating algorithms Sorting algorithms Set algorithms Relational algorithms

Retrieving or non-mutating algorithms adjacent_find() Count_if() Equal() Find_end() Mismatch() Search_n() count() equal() find() for_each() search()

Mutating algorithms Copy() Fill() Generate() Iter_swap() Remove_copy() Swap() Rotate() Unique() copy_backward() fill_n() generate_n() remove() replace() reverse() transform()

Sorting algorithms Binary_search() Inplace_merge() Make_heap() Partial_sort() equal_range() merge() nth_element() partition()

Pop_heap() Sort_heap() Upper_bound()

sort() stable_sort() sort_heap()

Set algorithms Set_difference() set_intersection() Set_symmetric_difference() Set_union()

Relational algorithms Equal() Lexicographical_compare() Max() Max_element() Min_element() Mismatch()

Iterators Iterators are like pointers and are used to access container elements. 5 types of iterators Input Output Forward Bidirectional Random

#include<iostream> //program using list container #include<list> #include<stdlib> // for using rand() Using namespace std; Void display(list<int> &lst) { list<int> :: iterator p; for(p = lst.begin(); p!=lst.end(); ++p) cout<< *p; } Void main() { List<int> list1; //empty list of 0 size List<int> list2(5); //empty list of 5 size For(p=list2.begin(); p!=list2.end; ++p) *p = rand()/100; Display(list1); Display(list2); List1.push_front(100); //add 2 elements at both the ends List1.push_back(200);

list2.pop_front(); List<int> listA List1.merge(list2); Display(list1); listA=list1; listA.sort(); listA.reverse(); Display(listA); }

Multiset #include<iostream> #include<vector> #include<string> #include<set> Using namespace std; Class student { int rollno; String name; Public : student(int trollno.string tname) { rollno=trollno; name= tname; } ostream &operator <<( ostream & tempout, student tstu) { return tempout<<tstu.rollno<<tstu.name<<endl; } bool operator <(student tstu) const // it is must for multiset { return(rollno<tstu.rollno); } }; Void main() { Multiset<student>multisetstudent; Student s1(123,mohit); Student s2(124,rohit); Student s3(125,kumar); Student s4(126,raj); Multisetstudent.insert(s1); Multisetstudent.insert(s2); Multisetstudent.insert(s3); Multisetstudent.insert(s1); // second time insert the same data Multisetstudent.insert(s2); Multisetstudent.insert(s3); Multiset<student>::iterator multisetstudentindex; For(multisetstudent=multisetstudent.begin();

multisetstudent != multisetstudent.end(); multisetstudentindex++) { cout<<*multisetstudentindex; } } Output 123 123 mohit 124 rohit 124 rohit 125 kumar 125 kumar mohit

Object serialization Objects can be persistent , which can be saved on the disk when a program exits and then can be restored when the program is restarted. This process of saving and restoring object is called serialization NAMESPACES The namespace is simply a declarative region. The purpose is to avoid name collision. The problem is solved by different enclosures. Each enclosure should have unique name known as namespace. Global namespace Fully qualified names and scope resolution operator std::string::nops;

nops is a member of string; string belongs to namespace std. Using syntax using declaration & using directive Using declaration Using directive #include<iostream> #include<iostream> Void main() Using namespace std; { Void main() Using std::cout; { Cout<<welcome; Cout<<welcome; } } Defining variables inside a namespace #include<iostream> #include<iostream> Using namespace std; Using namespace std; Namespace mylib // defining mylib namespace Namespace mylib { { int a; int a; int b; int b; } } Void main() using namespace mylib; { Void main() Using mylib::a; // using declaration { Using mylib::b; // using declaration cin>>a>>b Cin>>a>>b; Cout<<a<<b; Cout<<a<<b; } }

Defining functions inside the Namespace #include<iostream> Using namespace std; namespace mylib { int a=10; int b=20; void show() { cout<<a<<b; } } Using namespace mylib; // using directive Void main() { show(); } Defining classes inside the Namespace #include<iostream> Using namespace std; namespace mylib { class student { char *name; int roll; public: student( char *tname , int troll) { name = tname; roll = troll; } void display() { cout<<name<<roll; } }; } Using namespace mylib; Void main() { mylib::student s(James,1234); // calling constructor s.display(); } Declaring a function inside and defining outside the Namespace #include<iostream> Using namespace std; namespace mylib { int a,b; void get(); void put(); } Void mylib::get()

{ cin>>a>>b; } Void mylib::put() { cout<<a<<b; } Using namespace mylib; Void main() { get(); put(); } Declaring a member function inside and defining outside the Namespace #include<iostream> Using namespace std; namespace mylib { class student { int roll; char name[10]; public : void get(); void put(); }; } Void mylib::student::get() { cin>>mylib::student::roll; cin>.mylib::student::name; } Using namespace mylib; void main() { mylib::student s; s.get(); s.put(); } Extending the namespace #include<iostream> Using namespace std; namespace mylib { int a=10; } Namespace mylib { int b=20; } Using namespace mylib; Void main() { cout<<mylib::a<< <<mylib::b; } output 10 20 Using namespace in .h files

Void mylib::student::put() { cout<<mylib::student::roll; cout<<mylib::student::name; }

// save this file as mynamespace.h namespace myspace { int a=10; int b=20; } // save this file as sample.cpp #include<iostream> #include mynamespace.h Using namespace myspace; Void main() { cout<<myspace::a<<endl; cout<<myspace::b; } Output : 10 20 Unnamed namespaces #include<iostream> Using namespace std; namespace { int a=10; } Void main() { cout<<a; } The variable a is globally available in a file. It is not available to any other file linked with that file. Nested namespaces #include<iostream> Using namespace std; namespace outernamespace { int a=10; namespace innernamespace { int b=20; class sample { public : void show(); }; } } Void outernamespace::innernamespace::sample::show() { cout<<welcome; } Using namespace outernamespace::innernamespace; Void main() { Cout<<outernamespace::a; //accessing outer namespace Cout<<outernamespace::innernamespace::b; Outernamespace::innernamespace::sample s; s.show();

} Output : 10 20 welcome

Namespace aliases provide assumed name for namespace #include<iostream> Using namespace std; namespace outernamespace { int a=10; namespace innernamespace { int b=20; class sample { public : void show(); }; } } Void outernamespace::innernamespace::sample::show() { cout<<welcome; } Using namespace outernamespace::innernamespace; Void main() { Cout<<outernamespace::a; //accessing outer namespace Namespace NS= outernamespace::innernamespace; Cout<<NS::b; NS::sample s; s.show(); } Output : 10 20 welcome

STD namespace The term STD refers standard which is a namespace where the standard library routines are defined. using namespace std; It is important to note that the .h extension of the header file iostream should not be used while including that in a program if the program uses namespaces Koenig lookup

It is the ability of the complier to find out exact namespace of the object in use even when not specified by the programmer. The compiler uses an algorithm augment dependent lookup known as koenig lookup to find out object even if it is not mentioned either using directive or using declarative Example #include<iostream> Void main() { Using std::cout; Cout<<Hello; } In general cout works with << op. In the above program cout only defined with using declaration not << op. even though it is not defined the compiler searches the op using koenig lookup and qualify the operator.

Strings
ANSI C++ provides a new class called string. string is treated like container class with its own objects. For using string class we must include <string> in our program. It includes constructors, member functions and operators. String constructors String(); - for creating an empty string String(const char *str) creating a string terminated string. String(const string & str); - creating a object. from null string object from other string

Functions supported by string class append() at() Capacity() Empty() Erase() Insert() Max_size() Size() assign() begin() compare() end() find() length() replace() swap()

Additional functions Substr() for retrieving a substring. Find() finding a specified string Find_first_of() finding a location of 1st

occurrence of specified char(s)

Find_last_of() - finding a location of last char(s)

occurrence of specified

Operators for string objects = assignment + concatenation == equality < lass than > greater than >= greater than or equal to << output >> input

#include(iostream> #include<string> Using namespace std; Void main() { String s1(12345); String s2(abcde); Cout<<s1=<<s1; Cout<s2=<<s2; S1.insert(4,s2); Cout<<s1; s1.erase(4,5); Cout<<s1 S2.replace(1,3,s1); Cout<<s2; }

s1=12345 s2=abcde 1234abcde5 12345 a12345e

Arrange the strings in alphabetical order #include<iostream> #include<string> Using namespace std; Void main() { String str[10]; For(int i=0;i<10;i++) cin>>str[i]; For(i=0;i<10;i++) { For(int j=i+1;j<10;j++) { If(str[i] >= str[j] ) str[i].swap( [str[j] ) | | For(i=0i<10;i++)

cout<<str[i]<<endl; }

Вам также может понравиться