fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iterator> // For std::ostream_iterator
  5. #include <algorithm> // For std::copy
  6.  
  7. using namespace std;
  8.  
  9. class StringSet
  10. {
  11. public:
  12. StringSet(vector<string> str);
  13. void add(string s);
  14. void remove(int i);
  15. void clear();
  16. int length();
  17. void output(ostream& outs);
  18. private:
  19. vector<string> strarr;
  20. };
  21.  
  22. StringSet::StringSet(vector<string> str)
  23. {
  24. for(int k =0;k<str.size();k++)
  25. {
  26. strarr.push_back(str[k]);
  27. }
  28. }
  29. void StringSet::add(string s)
  30. {
  31. strarr.push_back(s);
  32. }
  33. void StringSet::remove(int i)
  34. {
  35. strarr.erase (strarr.begin()+(i-1));
  36. }
  37. void StringSet::clear()
  38. {
  39. strarr.erase(strarr.begin(),strarr.end());
  40. }
  41. int StringSet::length()
  42. {
  43. return strarr.size();
  44. }
  45. void StringSet::output( ostream& outs )
  46. {
  47. std::copy(strarr.begin(), strarr.end(), std::ostream_iterator<string>(outs, "\n"));
  48. }
  49.  
  50. int main()
  51. {
  52. vector<string> vstr;
  53. string s;
  54. for(int i=0;i<3;i++) // Changed the number of string to 3
  55. {
  56. cout<<"enter a string: ";
  57. cin>>s;
  58. vstr.push_back(s);
  59. }
  60. StringSet strset(vstr);
  61. strset.length();
  62. strset.add("hello");
  63. strset.output( std::cout ); // Write on the standard output
  64. strset.remove(3);
  65. // strset.empty(); Does not exist
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 3436KB
stdin
test1
test2
test3
stdout
enter a string: enter a string: enter a string: test1
test2
test3
hello