Help with removing an element of a string :(

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Help with removing an element of a string :(

 
0
  #1
Oct 11th, 2009
Hey guys.

This is really frustrating because it seems easy but I can't seem to find the correct function to remove this element of my string.

  1. void set::display() const
  2. // pre : none
  3. // post : displays the elements of this set enclosed in braces and
  4. // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
  5. {
  6. string s1 = ",";
  7. cout << "{";
  8. for (int i = 0; i < setlist.size(); i++)
  9. {
  10. if (setlist.at(i) == true)
  11. {
  12. cout << i;
  13. cout << s1;
  14. }
  15. }
  16. cout << "}";
  17. }


At the moment it will output something similar to this:
{1,2,3,} --> notice the , after the last digit?

It needs to be this:
{1,2,3} --> no ',' after the last digit.


Any help would be appreciated,
Regards.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #2
Oct 11th, 2009
Well at the end can't you just do an string erase on the last but one element?

string p = "{1,2,3,24,}";
p.erase(p.length()-2,1);
Last edited by iamthwee; Oct 11th, 2009 at 9:31 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster
 
0
  #3
Oct 11th, 2009
That's true, but how will I be able to declare string p? Because there is a for loop present.. so it would have to store the output as a string somehow which I'm not too sure how to go about

EDIT: The display() function is different every time because setlist is having elements added to it.. so the digits between the brackets will always be different.
Last edited by scantraXx-; Oct 11th, 2009 at 9:40 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
-2
  #4
Oct 11th, 2009
I think your easiest answer would be

vector<int>setlist;
setlist.push_back(3);
setlist.push_back(12);
setlist.push_back(4);


string s1 = ",";
cout << "{";
for (int i = 0; i < setlist.size(); i++)
{
cout << setlist[i];
if (i!=setlist.size()-1)
{

cout << s1;
}
}
cout << "}";

cin.get();
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #5
Oct 11th, 2009
oops i meant

  1. vector<bool>setlist;
  2. setlist.push_back(true);
  3. setlist.push_back(false);
  4. setlist.push_back(true);
  5. setlist.push_back(true);
  6.  
  7.  
  8. string s1 = ",";
  9. cout << "{";
  10. for (int i = 0; i < setlist.size(); i++)
  11. {
  12.  
  13. if (setlist.at(i) == true)
  14. {
  15. cout << i;
  16.  
  17. }
  18.  
  19. if (i!=setlist.size()-1)
  20. {
  21.  
  22. cout << s1;
  23. }
  24. }
  25. cout << "}";
  26.  
  27. cin.get();
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: Poincarre is an unknown quantity at this point 
Solved Threads: 1
Poincarre Poincarre is offline Offline
Newbie Poster

How aboult?

 
0
  #6
Oct 11th, 2009
1.
vector<bool>setlist;
2.
setlist.push_back(true);
3.
setlist.push_back(false);
4.
setlist.push_back(true);
5.
setlist.push_back(true);
6.

7.
int i

8.
string s1 = ",";
9.
cout << "{";
10.
for (i = 0; i < setlist.size(); i++)
11.
{
12.

13.
if (setlist.at(i) == true)
14.
{
15.
cout << i;
16.

17.
}
18.

19.
if (i!=setlist.size()-1)
20.
{
21.

22.
cout << s1;
23.
}
24.
}
25.
i--;
cout << "}";
26.

27.
cin.get();
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training
 
1
  #7
Oct 11th, 2009
I'd switch the order of printing around. First print a comma, then print the number.
This does require you to find the first number beforehand, though.

  1. void set::Display() const
  2. {
  3. cout << '{';
  4.  
  5. int i = 0;
  6. while(i < setlist.size() && setlist.at(i) != true)
  7. i++;
  8. if(i != setlist.size())
  9. cout << i;
  10. i++;
  11. for(i; i < setlist.size(); i++)
  12. if(setlist.at(i) == true)
  13. {
  14. cout << ", ";
  15. cout << i;
  16. }
  17.  
  18. cout << '}';
  19. }
Last edited by BeyondTheEye; Oct 11th, 2009 at 4:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,224
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 150
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #8
Oct 11th, 2009
Easy way out, just put a condition to check if "i" is at .size() - 1;
if not then print or else not print.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: Poincarre is an unknown quantity at this point 
Solved Threads: 1
Poincarre Poincarre is offline Offline
Newbie Poster
 
0
  #9
Oct 11th, 2009
1. void set::display() const
2. // pre : none
3. // post : displays the elements of this set enclosed in braces and
4. // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
5. {
6. string s1 = "";
7. cout << "{";
8. for (int i = 0; i < setlist.size(); i++, s1 = ",")
9. {
10. if (setlist.at(i) == true)
11. {
12. cout << s1;
13. cout << i;
14. }
15. }
16. cout << "}";
17. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC