d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me With A Vector Dilemma > 50 Fg For Simple Fix
Add Reply New Topic New Poll
Member
Posts: 23,516
Joined: Aug 3 2011
Gold: 3,575.00
Aug 14 2017 01:26pm
Code
void DELETE(char value, vector<char>set1)
{
for (int i = 0; i < set1.size(); i++)
{
if(set1[i] == value)
{
set1.erase( set1.begin(), set1.end(), i), set1.end() );
}
else if (set1[i] != value)
{
cout << "Set does not contain value";
}
}
}


Function name explains it all, just trying to erase a select value from the vector but it does not want to implement erase or anything of the sort. Help.
Member
Posts: 1,263
Joined: Feb 11 2008
Gold: 2,650.00
Aug 16 2017 02:17am
I see 3 issues:

1. Syntax of erase() call is broken:
http://www.cplusplus.com/reference/vector/vector/erase/

I would suggest you use an iterater over the vector, which has the added benefit of making the erase() call more convenient since you'll already have the iterator parameter it's expecting:
https://stackoverflow.com/questions/4713131/removing-item-from-vector-while-iterating
If you do, make sure you look at wilx's answer, which uses the return value of the erase() call to update the iterator. This also resolves the pesky issue of the remaining indexes being "shifted left" each time you delete an item in front of them.


2. The cout is being printed for each element that doesn't match "value". You'd be better off setting a flag true if-and-only-if a deletion is performed; if, at the end of the vector, the flag is still false, print the message.


3. Presumably, you want this function to modify a vector for later processing. If so, you'll want to pass the vector "by reference":
https://stackoverflow.com/questions/15890427/passing-vector-by-reference

This will allow you to modify the vector passed in, rather than modifying a copy that is deleted as soon as the function returns.
Member
Posts: 23,516
Joined: Aug 3 2011
Gold: 3,575.00
Aug 16 2017 01:26pm
Quote (postmortemvox @ Aug 16 2017 01:17am)
I see 3 issues:

1. Syntax of erase() call is broken:
http://www.cplusplus.com/reference/vector/vector/erase/

I would suggest you use an iterater over the vector, which has the added benefit of making the erase() call more convenient since you'll already have the iterator parameter it's expecting:
https://stackoverflow.com/questions/4713131/removing-item-from-vector-while-iterating
If you do, make sure you look at wilx's answer, which uses the return value of the erase() call to update the iterator. This also resolves the pesky issue of the remaining indexes being "shifted left" each time you delete an item in front of them.


2. The cout is being printed for each element that doesn't match "value". You'd be better off setting a flag true if-and-only-if a deletion is performed; if, at the end of the vector, the flag is still false, print the message.


3. Presumably, you want this function to modify a vector for later processing. If so, you'll want to pass the vector "by reference":
https://stackoverflow.com/questions/15890427/passing-vector-by-reference

This will allow you to modify the vector passed in, rather than modifying a copy that is deleted as soon as the function returns.


Although I solved the problem, I am a man of my word as this was the solution x)
8/16/2017, 3:26:59 PM Sent -50.00 (4,350.01 -> 4,300.01) to postmortemvox (2,000.00 -> 2,050.00) C++ Vector Dilemma Answer

This post was edited by Cocoo on Aug 16 2017 01:27pm
Member
Posts: 8,599
Joined: Jul 1 2012
Gold: 10,483.00
Aug 19 2017 09:16am
if you need help i made more then 10 games and alots program

i do lua, c ,c++, python, i'm graphist and have 18 years of school, but i'm french speaker/writer
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll