Advertisement
nex036ara

stl

Mar 4th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include<vector>
  5. #include <list>
  6. #include<algorithm>
  7. #include <fstream>
  8.  
  9.  
  10. using namespace std;
  11.  
  12.     static vector<int> indexNumbers;
  13.     static list<string> studentNames;
  14.     static map<int,string> mapNumbersToStrings;
  15.     static map<string,int> mapStringsToNumbers;
  16.  
  17.     void addStudent();
  18.     void loadStudent();
  19.     void addStudentSpecial();
  20.  
  21.  
  22.     void printIndexNumbers();
  23.     void printStudentNames();
  24.     void printindexAndNames();
  25.  
  26.     void makeIndexToNameMap();
  27.     void printIndexToNameMap();
  28.  
  29.     void makeNameToIndexMap();
  30.    
  31.  
  32.  
  33. int main(int argc, char *argv[]) {
  34.  
  35.  
  36.     //addStudent();
  37.     //addStudentSpecial();
  38.     //loadStudent();
  39.  
  40.     //printIndexNumbers();
  41.     //printStudentNames();
  42.     //printindexAndNames();
  43.  
  44.     //makeIndexToNameMap();
  45.     //printIndexToNameMap();
  46.     //makeNameToIndexMap();
  47.    
  48.  
  49.     //printDesiredIndex(string name);
  50.     //printDesiredName(int x);
  51.    
  52.    
  53.     addStudent();
  54.     addStudent();
  55.     makeIndexToNameMap();
  56.     printIndexToNameMap();
  57.     return 0;
  58. }
  59.  
  60.  
  61. void loadStudent() {
  62.     ifstream inputFile("baza_studenata.txt");
  63.     if(inputFile.bad()) {
  64.     cout<<"bad_file_name!"<<endl;
  65.     return;
  66.     }
  67.     int index;
  68.     string ime;
  69.     string prezime;
  70.  
  71.     do{
  72.     inputFile>>ime;
  73.     inputFile>>prezime;
  74.     inputFile>>index;
  75.  
  76.     ime = ime + " "+ prezime;
  77.     indexNumbers.push_back(index);
  78.     studentNames.push_back(ime);
  79.  
  80.     }while(inputFile.eof()==false);
  81.  
  82. }
  83. void printIndexNumbers(){
  84.  
  85.     vector<int>::reverse_iterator it1;
  86.     for(it1 = indexNumbers.rbegin(); it1!= indexNumbers.rend(); ++it1) {
  87.     cout<< *it1<<endl;
  88.     }
  89. }
  90. void printStudentNames(){
  91.     list<string>::iterator it2;
  92.     for(it2 = studentNames.begin(); it2!=studentNames.end(); ++it2) {
  93.     cout<< *it2<<endl;
  94.     }
  95. }
  96. void printindexAndNames(){
  97.     vector<int>::iterator it1;
  98.     list<string>::iterator it2;
  99.     for(it1 = indexNumbers.begin(), it2 = studentNames.begin();  it1!= indexNumbers.end(),it2!=studentNames.end();
  100.          ++it1, ++it2) {
  101.              cout<< *it2 <<" "<<*it1<<endl;
  102.     }
  103. }
  104. void addStudent() {
  105.  
  106.     int x;
  107.     cout<<"Broj indexa: " <<endl;
  108.     cin>>x;
  109.     string ime,prezime;
  110.     cout<<"Ime: "<<endl;
  111.     cin>>ime;
  112.     cout<<"Prezime: "<<endl;
  113.     cin>>prezime;
  114.  
  115.     indexNumbers.push_back(x);
  116.     ime = ime+" "+prezime;
  117.     studentNames.push_back(ime);
  118. }
  119. void addStudentSpecial() {
  120.  
  121.     //metoda koja dodaje Marka Kraljevica(23456) odmah iza indexa 12345
  122.         string ime = "Marko Kraljevic";
  123.         int index = 23456;
  124.            
  125.     vector<int>::iterator it1;
  126.     list<string>::iterator it2;
  127.     for(it1 = indexNumbers.begin(), it2 = studentNames.begin();  it1!= indexNumbers.end(),it2!=studentNames.end();
  128.          ++it1, ++it2) {
  129.              if(*it1==12345) {
  130.              ++it1; ++it2;
  131.              studentNames.insert(it2,ime);
  132.              indexNumbers.insert(it1,index);
  133.                  break;
  134.              
  135.              }
  136.     }
  137. }
  138. void makeIndexToNameMap(){
  139. //napraviti mapu preslikavanja brojeva indeksa na imena
  140.     vector<int>::iterator it1;
  141.     list<string>::iterator it2;
  142.  
  143.  
  144.     for(it1 = indexNumbers.begin(), it2 = studentNames.begin(); it1!=indexNumbers.end(),it2!=studentNames.end();
  145.         ++it1, ++it2) {
  146.         mapNumbersToStrings[*it1] = *it2; //izlazak iz funkcije,unistavanje mape!
  147.        
  148.     }
  149.  
  150.  
  151. }
  152. void printIndexToNameMap(){
  153.    
  154.     map<int,string>::iterator it3;
  155.     for(it3 =mapNumbersToStrings.begin(); it3!=mapNumbersToStrings.end(); ++it3 ) {
  156.         cout<< it3->first<<" "<<it3->second<<endl;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement