fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. vector<string> user;
  7.  
  8. void doTest(string newuser) {
  9.  
  10. if (user.size() < 1){
  11. cout << "vector is empty, adding " << newuser << "\n";
  12. user.push_back(newuser);
  13. }
  14.  
  15. for (int i = 0; i < user.size(); ++i){
  16. string name = user[i];
  17. if (name == newuser) {
  18. cout << "index " << i << ": " << newuser << " found, breaking loop\n";
  19. break;
  20. }
  21. else {
  22. cout << "index " << i << ": " << name << ", adding " << newuser << "\n";
  23. user.push_back(newuser);
  24. }
  25. }
  26. }
  27.  
  28. void printUsers()
  29. {
  30. for(auto &u : user) {
  31. cout << u << "\n";
  32. }
  33. cout << "\n";
  34. }
  35.  
  36. int main()
  37. {
  38. user.push_back("dbotting");
  39. user.push_back("egomez");
  40.  
  41. cout << "before tests:\n";
  42. printUsers();
  43.  
  44. cout << "running test 1:\n";
  45. doTest("tongyu");
  46. cout << "after test 1:\n";
  47. printUsers();
  48.  
  49. cout << "running test 2:\n";
  50. doTest("tongyu");
  51. cout << "after test 2:\n";
  52. printUsers();
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
before tests:
dbotting
egomez

running test 1:
index 0: dbotting, adding tongyu
index 1: egomez, adding tongyu
index 2: tongyu found, breaking loop
after test 1:
dbotting
egomez
tongyu
tongyu

running test 2:
index 0: dbotting, adding tongyu
index 1: egomez, adding tongyu
index 2: tongyu found, breaking loop
after test 2:
dbotting
egomez
tongyu
tongyu
tongyu
tongyu