Inteligent vector

Pages: 12
the compiler says that order,stock and name wasnt declacared in this scope..
the compiler says that order,stock and name wasnt declacared in this scope..

Look back at your post of May 16, 2015 at 2:13pm to see how you should access the member of a struct. You managed it that time so why not this time around?

Andy

PS From the reference entry on vector::erase()
http://www.cplusplus.com/reference/vector/vector/erase/

Iterators, pointers and references pointing to position (or first) and beyond are invalidated, with all iterators, pointers and references to elements before position (or first) are guaranteed to keep referring to the same elements they were referring to before the call.

So once you called erase() on an iterator you can't use it again. So you will need to rework your for loop once you sort out your struct member access.

What you will need to do it use the iterator that vector::erase() returns to work out what to do next.

Or you could use the remove_it() aglorithm
http://www.cplusplus.com/reference/algorithm/remove_if/

If you do, you will also require vector::erase (you need to use the "erase-remove idiom".)

Erase–remove idiom
http://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
Last edited on
So what I would have to do is, define a logic function that return to me whatever is logic( in my case, any member is empty,like boll SortByorder), if It's empty erase it, and once It's erased , set again the iterator....It's easy to say, but difficult to code....but I think It's what I have to..
I think I have been ablot to manage it, I have defined a function to return 1 when any empty condition happens...and I have compared the size of the vector<bench> when I has been erased with the size before being erased, If It is different, the iterator must be constructed again....surely there will be a more elegant way to do it.....

I'm going to leave here, the part of the code what matters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool DetectEmpty(const bench& o){
		return(o.name == "" or o.stock == 0 or o.order == 0);
	
	}
	
	int  EraseStruct(vector<bench> &test){
		for(vector<bench>::iterator it = test.begin();it != test.end();++it){
					bench& CeroItem = (*it);
					if(DetectEmpty(CeroItem)){
						test.erase(it);
						
						
					}
			}
		return test.size();
		}


int main{
......

int a = 1;
	int b = 0;
	while( a != b){
		a = WorkShop.size();
		b = EraseStruct(WorkShop);
	}
.......




now I dont what to do more to learn something, what could I try??
This code shows basic iterator usage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;

void dump(const vector<int>& vec)
{
    cout << "[count=" << vec.size() << "]";
    for(auto i : vec) // using C++11 range for loop here
        cout << " " << i;
    cout << "\n";
}

// 3.1415926535897932384626433832795
// PI stolen from Calc.exe
int main()
{
    // assuming compiler is C++11 compiliant so can
    // use initializer list.
    vector<int> vec = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3};

    dump(vec);
    cout << "\n";

    cout << "Remove all 5s\n";
    // in C++11 could use auto instead of vector<int>::iterator for iter's type
    for(vector<int>::iterator iter = vec.begin(); iter != vec.end(); /*don't increment here*/)
    {
        if(5 == *iter)
            iter = vec.erase(iter); // if this element is 5 delete it, replacing iterator
        else
            ++iter; // otherwise increment iterator
    }
    cout << "\n";

    dump(vec);
    cout << "\n";

    return 0;
}


Andy

PS With the remove() and remove_if() algorithms from <algorithm> it's:

1
2
3
    cout << "Remove all 9s\n";
    vec.erase(remove(vec.begin(), vec.end(), 9), vec.end());
    cout << "\n";


and

1
2
3
    cout << "Remove all other odd numbers\n";
    vec.erase(remove_if(vec.begin(), vec.end(), is_odd), vec.end());
    cout << "\n";


where is_odd() is:

1
2
3
4
bool is_odd(int val)
{
    return (0 != (val % 2));
}


See
http://www.cplusplus.com/reference/algorithm/remove/
http://www.cplusplus.com/reference/algorithm/remove_if/

for info on how these algorithms work, inc. why you need to call erase() as well when you want to actually remove elements from a vector.
Last edited on
thanks!!!, for my case I could do
1
2
3
4
5
6
7

bool is_odd(vector<bench> val)
{

           return(val.name = " " or val.stock == 0 or val.order == 0);
}
Nope

You pass the type of an element (in your case a bench) not the type of the vector:

bool is_odd(const bench& val)

(as you're passing a struct type you should use a const ref, rather than passing it by value.)

Andy
Last edited on
for(auto i : vec) ...It seems like with auto I dont have to set statements and I dont have to define the type,do I?

I'm trying to mix your example with remove(), first I want to use a vector and remove all 5's, and later I'll try to remove 5's from vector<struct>...I got this code without erros but...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



// 3.1415926535897932384626433832795
// PI stolen from Calc.exe
int main()
{
    // assuming compiler is C++11 compiliant so can
    // use initializer list.
    vector<int> vec = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3};
	vector<int>::iterator  Pvec; 
	
	
	Pvec = remove(vec.begin(),vec.end(),5);
   
    cout << "\n";

    cout << "Remove all 5s\n";
    // in C++11 could use auto instead of vector<int>::iterator for iter's type
	for(vector<int>::iterator Pvec2 = Pvec;Pvec2 != vec.end();++Pvec2){
			cout<< *Pvec2;
			cout << "\n";
		}
    


    cout << "\n";

    return 0;
}


I'll keep trying...
I know what's happening...remove() return an iterator to the end of the new size...so It has rested two position and It starts to read since there, so I got 3 position more to be read...
The trouble is, I f I want to delete the last value in the vector, with remove It cant be done, because that iterator never return the last value..with this code, the last 3 won't be removed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



// 3.1415926535897932384626433832795
// PI stolen from Calc.exe
int main()
{
    // assuming compiler is C++11 compiliant so can
    // use initializer list.
    vector<int> vec = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3};
	
	
	
	 remove(vec.begin(),vec.end(),3);
		
	
   
    cout << "\n";

    cout << "Remove all 5s\n";
    // in C++11 could use auto instead of vector<int>::iterator for iter's type
	for(vector<int>::iterator Pvec =vec.begin() ;Pvec != vec.end();++Pvec){
			cout<< *Pvec;
			cout << "\n";
		}
    


    cout << "\n";

    return 0;
}

I told you how you need to use the remove algorithm. With vector::erase(), as I did in my code snippets.

Also see the documentation I pointed you to above.

There's nothing more I can add on this issue.

Andy
Topic archived. No new replies allowed.
Pages: 12