{"id":19083276,"url":"https://github.com/psingh12354/c-stl","last_synced_at":"2025-04-30T08:48:55.787Z","repository":{"id":185587900,"uuid":"280404324","full_name":"Psingh12354/C-STL","owner":"Psingh12354","description":"Here I upload my C++ STL program which i have done during Covid-19.","archived":false,"fork":false,"pushed_at":"2021-06-18T11:34:28.000Z","size":1159,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-08-02T13:09:53.555Z","etag":null,"topics":["basic","cpp","examples","explaination","insertion","notes","pair-template","practice-programming","stl","vector"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Psingh12354.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-07-17T11:13:33.000Z","updated_at":"2023-08-02T13:09:58.291Z","dependencies_parsed_at":null,"dependency_job_id":"07a68dba-5445-47fd-be43-b021b496bdfe","html_url":"https://github.com/Psingh12354/C-STL","commit_stats":null,"previous_names":["psingh12354/c-stl"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Psingh12354%2FC-STL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Psingh12354%2FC-STL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Psingh12354%2FC-STL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Psingh12354%2FC-STL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Psingh12354","download_url":"https://codeload.github.com/Psingh12354/C-STL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223780073,"owners_count":17201287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["basic","cpp","examples","explaination","insertion","notes","pair-template","practice-programming","stl","vector"],"created_at":"2024-11-09T02:46:47.546Z","updated_at":"2024-11-09T02:46:48.284Z","avatar_url":"https://github.com/Psingh12354.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=center\u003e\u003cb\u003eC-STL\u003c/b\u003e\u003c/h1\u003e\n\u003ch3\u003e\u003ci\u003eThese Note are from diffrent sites which i copy it here for my learning.\u003c/i\u003e\u003c/h3\u003e\n\n# What are Containers in STL?\n\u003cb\u003eContainers Library in STL gives us the Containers\u003c/b\u003e, which in simplest words, can be described as the objects used to contain data or rather collection of object. Containers help us to implement and replicate simple and complex data structures very easily like arrays, list, trees, associative arrays and many more.\n\nThe containers are implemented as generic class templates, means that a container can be used to hold different kind of objects and they are dynamic in nature!\n\nFollowing are some common containers :\n\n- vector : replicates arrays\n- queue : replicates queues\n- stack : replicates stack\n- priority_queue : replicates heaps\n- list : replicates linked list\n- set : replicates trees\n- map : associative arrays\n\n## Classification of Containers in STL\nContainers are classified into four categories :\n\n### Sequence containers : Used to implement data structures that are sequential in nature like arrays(array) and linked list(list).\n### Associative containers : Used to implement sorted data structures such as map, set etc.\n### Unordered associative containers : Used to implement unsorted data structures.\n### Containers adaptors : Used to provide different interface to the sequence containers.\n\n## Using Container Library in STL\nBelow is an example of implementing linked list, first by using structures and then by list containers.\n```\n#include \u003ciostream\u003e\n\nstruct node\n{\n    int data;\n    struct node * next;\n}\n\nint main ()\n{\n    struct node *list1 = NULL;\n}\n```\nThe above program is only creating a list node, no insertion and deletion functions are defined, to do that, you will have to write more line of code.\n\nNow lets see how using Container Library simplifies it. When we use list containers to implement linked list we just have to include the list header file and use list constructor to initialize the list.\n```\n#include \u003ciostream\u003e\n#include \u003clist\u003e\n\nint main ()\n{\n    list\u003cint\u003e list1; \n}\n```\nAnd that's it! we have a list, and not just that, the containers library also give all the different methods which can be used to perform different operations on list such as insertion, deletion, traversal etc.\n\nThus you can see that it is incredibly easy to implement data structures by using Container library.\n\nSite Link : https://www.studytonight.com/cpp/stl/stl-overview-of-containers#:~:text=Containers%20Library%20in%20STL%20gives,associative%20arrays%20and%20many%20more.\n\n## PAIR Template in STL\nNOTE: Although Pair and Tuple are not actually the part of container library but we'll still discuss them as they are very commonly required in programming competitions and they make certain things very easy to implement.\n ## SYNTAX of pair is:\n```\npair\u003cT1,T2\u003e  pair1, pair2 ;\n```\nThe above code creates two pairs, namely pair1 and pair2, both having first object of type T1 and second object of type T2.\n\nNow T1 will be referred as first and T2 will be referred as second member of pair1 and pair2.\n\n## Pair Template: Some Commonly used Functions\nHere are some function for pair template :\n\n### Operator = : assign values to a pair.\n### swap : swaps the contents of the pair.\n### make_pair() : create and returns a pair having objects defined by parameter list.\n### Operators( == , != , \u003e , \u003c , \u003c= , \u003e= ) : lexicographically compares two pairs.\n\n## Program demonstrating PAIR Template\n```\n#include \u003ciostream\u003e\n\nusing namespace std;    \n    \nint main ()\n{\n   pair\u003cint,int\u003e pair1, pair3;    //creats pair of integers\n   pair\u003cint,string\u003e pair2;    // creates pair of an integer an a string\n    \n   pair1 = make_pair(1, 2);     // insert 1 and 2 to the pair1\n   pair2 = make_pair(1, \"Studytonight\") // insert 1 and \"Studytonight\" in pair2\n   pair3 = make_pair(2, 4)\n   cout\u003c\u003c pair1.first \u003c\u003c endl;  // prints 1, 1 being 1st element of pair1\n   cout\u003c\u003c pair2.second \u003c\u003c endl; // prints Studytonight\n\n   if(pair1 == pair3)\n        cout\u003c\u003c \"Pairs are equal\" \u003c\u003c endl;\n   else\n        cout\u003c\u003c \"Pairs are not equal\" \u003c\u003c endl;\n   \n   return 0;\n}\n```\nSite Link : https://www.studytonight.com/cpp/stl/stl-pair-template\n\n## C++ My Codes : \n\n## Calculator \n``` \n#include \u003ciostream\u003e\nusing namespace std;\n\ntemplate \u003cclass T\u003e\nclass Calculator\n{\nprivate:\n\tT num1, num2;\n\t\npublic:\n\tCalculator(T n1, T n2)\n\t{\n\t\tnum1 = n1;\n\t\tnum2 = n2;\n\t}\n\t\n\tvoid displayResult()\n\t{\n\t\tcout \u003c\u003c \"Numbers are: \" \u003c\u003c num1 \u003c\u003c \" and \" \u003c\u003c num2 \u003c\u003c \".\" \u003c\u003c endl;\n\t\tcout \u003c\u003c \"Addition is: \" \u003c\u003c add() \u003c\u003c endl;\n\t\tcout \u003c\u003c \"Subtraction is: \" \u003c\u003c subtract() \u003c\u003c endl;\n\t\tcout \u003c\u003c \"Product is: \" \u003c\u003c multiply() \u003c\u003c endl;\n\t\tcout \u003c\u003c \"Division is: \" \u003c\u003c divide() \u003c\u003c endl;\n\t}\n\t\n\tT add() { return num1 + num2; }\n\t\n\tT subtract() { return num1 - num2; }\n\t\n\tT multiply() { return num1 * num2; }\n\t\n\tT divide() { return num1 / num2; }\n};\n\nint main()\n{\n\tCalculator\u003cint\u003e intCalc(2, 1);\n\tCalculator\u003cfloat\u003e floatCalc(2.4, 1.2);\n\t\n\tcout \u003c\u003c \"Int results:\" \u003c\u003c endl;\n\tintCalc.displayResult();\n\t\n\tcout \u003c\u003c endl \u003c\u003c \"Float results:\" \u003c\u003c endl;\n\tfloatCalc.displayResult();\n\t\n\treturn 0;\n}\n```\n## Container \n```\n/*\nContainer library is collection of classes\nThe container are implemented as genric classes\n*/\n#include\u003ciostream\u003e\n#include\u003clist\u003e\nusing namespace std;\n\nint main()\n{\n\tlist\u003cint\u003e list1;\n\treturn 0;\n}\n```\n## Function Templates\n\nTemplates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.\nTemplates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.\nThe concept of templates can be used in two different ways:\nFunction Templates\nClass Templates\n \n**Stl stand for standard template library**\n\u003cfloat\u003e this method tell which the value you are using from template\nContainters are used to manage collections of object of a certain kind\nvector can be used for creating dynamic array\nFunction Templates\nA function template works in a similar to a normal function, with one key difference.\nA single function template can work with different data types at once but, a single normal function can only work with one set of data types.\nNormally, if you need to perform identical operations on two or more types of data, you use function overloading to create two functions with the required function declaration.\nHowever, a better approach would be to use function templates because you can perform the same task writing less and maintainable code.\nHow to declare a function template?\nA function template starts with the keyword template followed by template parameter/s inside  \u003c \u003e which is followed by function declaration.\n\n```\ntemplate \u003cclass T\u003e\nT someFunction(T arg)\n{\n   ... .. ...\n}\n```\n\nIn the above code, T is a template argument that accepts different data types (int, float), and class is a keyword.\nYou can also use keyword typename instead of class in the above example.\nWhen, an argument of a data type is passed to someFunction( ), compiler generates a new version of someFunction() for the given data type.\n\n### Code    \n```\n#include\u003ciostream\u003e\nusing namespace std;\ntemplate \u003cclass Temp\u003e\nTemp Large(Temp n1,Temp n2)\n{\n\treturn (n1\u003en2)?n1:n2;\n}\nint main()\n{\n\tint i1,i2;\n\tfloat f1,f2;\n\tchar c1,c2;\n\tcout\u003c\u003c\"Enter the two integer : \\n\";\n\tcin\u003e\u003ei1\u003e\u003ei2;\n\tcout\u003c\u003cLarge(i1,i2)\u003c\u003c\" is largest\\n\";\n\tcout\u003c\u003c\"Enter two floating point number : \\n\";\n\tcin\u003e\u003ef1\u003e\u003ef2;\n\tcout\u003c\u003cLarge(f1,f2)\u003c\u003c\" is largest\\n\";\n\tcout\u003c\u003c\"Enter two character : \\n\";\n\tcin\u003e\u003ec1\u003e\u003ec2;\n\tcout\u003c\u003cLarge(c1,c2)\u003c\u003c\" is largest\\n\";\n\treturn 0;\n}\n```\n\n## Pair Template\n\nTemplates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.\nTemplates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.\nThe concept of templates can be used in two different ways:\nFunction Templates\n\n## Class Templates\n \nStl stand for standard template library\n\u003cfloat\u003e this method tell which the value you are using from template\nContainters are used to manage collections of object of a certain kind\nvector can be used for creating dynamic array\nFunction Templates\nA function template works in a similar to a normal function, with one key difference.\nA single function template can work with different data types at once but, a single normal function can only work with one set of data types.\nNormally, if you need to perform identical operations on two or more types of data, you use function overloading to create two functions with the required function declaration.\nHowever, a better approach would be to use function templates because you can perform the same task writing less and maintainable code.\nHow to declare a function template?\nA function template starts with the keyword template followed by template parameter/s inside  \u003c \u003e which is followed by function declaration.\n\n```\ntemplate \u003cclass T\u003e\nT someFunction(T arg)\n{\n   ... .. ...\n}\n```\n\nIn the above code, T is a template argument that accepts different data types (int, float), and class is a keyword.\nYou can also use keyword typename instead of class in the above example.\nWhen, an argument of a data type is passed to someFunction( ), compiler generates a new version of someFunction() for the given data type.\n\n\n```\n#include\u003ciostream\u003e\nusing namespace std;\ntemplate \u003cclass Temp\u003e\nTemp Large(Temp n1,Temp n2)\n{\n\treturn (n1\u003en2)?n1:n2;\n}\nint main()\n{\n\tint i1,i2;\n\tfloat f1,f2;\n\tchar c1,c2;\n\tcout\u003c\u003c\"Enter the two integer : \\n\";\n\tcin\u003e\u003ei1\u003e\u003ei2;\n\tcout\u003c\u003cLarge(i1,i2)\u003c\u003c\" is largest\\n\";\n\tcout\u003c\u003c\"Enter two floating point number : \\n\";\n\tcin\u003e\u003ef1\u003e\u003ef2;\n\tcout\u003c\u003cLarge(f1,f2)\u003c\u003c\" is largest\\n\";\n\tcout\u003c\u003c\"Enter two character : \\n\";\n\tcin\u003e\u003ec1\u003e\u003ec2;\n\tcout\u003c\u003cLarge(c1,c2)\u003c\u003c\" is largest\\n\";\n\treturn 0;\n}\n```\n## Swap Templates\n\n```\n#include\u003ciostream\u003e\nusing namespace std;\ntemplate \u003ctypename T\u003e\nvoid Swap(T \u0026n1,T \u0026n2)\n{\n\tT temp;\n\ttemp=n1;\n\tn1=n2;\n\tn2=temp;\n}\nint main()\n{\n\tint i1=1,i2=2;\n\tfloat f1=1.1,f2=2.2;\n\tchar c1='a',c2='b';\n\n\n\tcout \u003c\u003c \"Before passing data to function template.\\n\";\n\tcout \u003c\u003c \"i1 = \" \u003c\u003c i1 \u003c\u003c \"\\ni2 = \" \u003c\u003c i2;\n\tcout \u003c\u003c \"\\nf1 = \" \u003c\u003c f1 \u003c\u003c \"\\nf2 = \" \u003c\u003c f2;\n\tcout \u003c\u003c \"\\nc1 = \" \u003c\u003c c1 \u003c\u003c \"\\nc2 = \" \u003c\u003c c2;\n\n\tSwap(i1, i2);\n\tSwap(f1, f2);\n\tSwap(c1, c2);\n\n        cout \u003c\u003c \"\\n\\nAfter passing data to function template.\\n\";\n\tcout \u003c\u003c \"i1 = \" \u003c\u003c i1 \u003c\u003c \"\\ni2 = \" \u003c\u003c i2;\n\tcout \u003c\u003c \"\\nf1 = \" \u003c\u003c f1 \u003c\u003c \"\\nf2 = \" \u003c\u003c f2;\n\tcout \u003c\u003c \"\\nc1 = \" \u003c\u003c c1 \u003c\u003c \"\\nc2 = \" \u003c\u003c c2;\n\n\treturn 0;\n\t\n}\n```\n\n## Vector Size\n\n```\n#include\u003ciostream\u003e\n#include\u003cvector\u003e\nusing namespace std;\nint main()\n{\n\tvector\u003cint\u003e vec;\n\tint i;\n\tcout\u003c\u003c\"Vecotr size : \"\u003c\u003cvec.size()\u003c\u003c\"\\n\";\n\tfor(i=0;i\u003c5;i++)\n\t{\n\t\tvec.push_back(i);\n\t}\n\tcout\u003c\u003c\"Extended vector size : \"\u003c\u003cvec.size()\u003c\u003c\"\\n\";\n\tfor(i=0;i\u003c5;i++)\n\t{\n\t\tcout\u003c\u003c\"Value of vec [\"\u003c\u003ci\u003c\u003c\"] = \"\u003c\u003cvec[i]\u003c\u003c\"\\n\";\n\t}\n\tvector\u003cint\u003e::iterator v=vec.begin();\n\twhile(v!=vec.end()){\n\t\tcout\u003c\u003c\"Value of v = \"\u003c\u003c*v\u003c\u003c\"\\n\";\n\t\tv++;\n\t}\n\treturn 0;\n}\n```\n## Array\n\n```\n/*\nArray is collection of similar element\nSTL has predefine class array\nlike this\narray\u003cint,4\u003e obj;\nHere a array is formed with side 4\nto use this you need to include array\ntemplate\u003cplace holder\u003e\nclass array{\n};\n*/\n```\n## Code\n```\n#include\u003ciostream\u003e\n#include\u003carray\u003e\nusing namespace std;\nint main()\n{\narray\u003cint,4\u003e data1={2,5,6,7};\n// use this extra array with swap\narray\u003cint,4\u003e data2={1,4,5,9};\n// we can also initilize it here\n// like this\n// array\u003cint,4\u003e data={2,5,6,8};}\n// it show some error\n// to fix it click on setting\n// if you are using dev then check on tool comiler option read the error and copy it on genneral add by tick\ncout\u003c\u003cdata1.at(1)\u003c\u003cendl; // it show index array start from 0\ncout\u003c\u003cdata1[2]\u003c\u003cendl;\ncout\u003c\u003cdata1.front()\u003c\u003cendl;\ncout\u003c\u003cdata1.back()\u003c\u003cendl;\nfor(int i=0;i\u003c=data1.size();i++)\n{\n\tcout\u003c\u003cdata1[i]\u003c\u003c\" \";\n}\ncout\u003c\u003c\"\\n\";\ndata1.fill(10);\nfor(int i=0;i\u003c=data1.size();i++)\n{\n\ncout\u003c\u003cdata1[i]\u003c\u003c\" \";\n// to store 1 element in hole array\n}\ncout\u003c\u003c\"\\n\";\n// swap\ndata1.swap(data2);\nfor(int i=0;i\u003c=data1.size();i++)\n{\ncout\u003c\u003cdata1[i]\u003c\u003c\" \";\n}\ncout\u003c\u003c\"\\n\";\nfor(int i=0;i\u003c=data2.size();i++)\n{\ncout\u003c\u003cdata2[i]\u003c\u003c\" \";\n}\ncout\u003c\u003c\"\\n\";\n// to find the size\ncout\u003c\u003cdata1.size();\n}\n```\n\n\n\n\n#### After learning BASIC DATA-STRUCTURES AND ALGORITHMS, one can Start Competitative programming.\nHere is a refernce on getting started with the competitative  progaramming: http://sportprogramming.blogspot.com/2014/07/getting-started-with-sport-of.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpsingh12354%2Fc-stl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpsingh12354%2Fc-stl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpsingh12354%2Fc-stl/lists"}