In cantainer of vectors how i can find the number of objects without loop?

Nov 25, 2013 at 8:07am
Hello.
how i can find the 5 without loop?

1
2
3
4
5
6
7
8
9
10
11
12
vector<int>i;
vector<int>j;
i.push_back(1);
i.push_back(2);
j.push_back(3);
j.push_back(4);
j.push_back(5);
vector<vector<int>*>k;
k.push_back(&i);
k.push_back(&j);
cout<<"size of k is .."<<k.size();
cout<<"\nsize of all elements of k is .."<<.....// how i can find the 5 without loop? 

Nov 25, 2013 at 10:34am
this make no sense: you want to search for value without actually searching? Or you just want to use index?

http://www.cplusplus.com/reference/vector/vector/?kw=vector
Nov 25, 2013 at 3:07pm
Nov 26, 2013 at 7:10pm
Hello, i just come in...
I don't want to search for a value, the 5 is the number of elements in all vectors ...
Thank you for reply.
I will try seven50 link...
Last edited on Nov 26, 2013 at 7:11pm
Nov 26, 2013 at 7:15pm
Nov 27, 2013 at 7:57am
Yes but not....
We have two vectors on one vector container...
The size of all int are 5.
If each container have objects with different types then what?
That is the question...
Maybe http://www.cplusplus.com/reference/numeric/accumulate/ is the answer, i will see when find little time ...

Thank you.

Nov 27, 2013 at 8:22am
The size of all int are 5

what?

If, as you say, vector has always 5 elements then why using vector at all?

k.at(0) is vector i

k.at(1) is vector j

k.at(0)->at(0) is value 1. from vector i

k.at(0)->at(1) is value 1. from vector i

k.at(1)->at(0) is value 3. from vector j

k.at(1)->at(1) is value 4. from vector j

k.at(1)->at(2) is value 5. from vector j

if you want to get size off all elements in vector k you have to use loop because you dont know how many vector pointers is being hold in vector k:
1
2
3
unsigned size=0;
for (unsigned i=0; i<k.size(); i++)
size += k.at(i)->size() ;


and even if you dont want to use loop, there will be one in some function you might find, but hidden inside.

ps. this post cant get formated correctly ;x
Last edited on Nov 27, 2013 at 8:26am
Nov 27, 2013 at 2:06pm
http://www.cplusplus.com/reference/vector/vector/size/
Returns the number of elements in the vector.
This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.
Last edited on Nov 27, 2013 at 2:06pm
Nov 27, 2013 at 4:53pm
Vector has a member function: unsigned int size() const. It will return the number of elements the vector contains (literally; 0 = no elements, (so your_vector.size() - 1) would be the very last element).

If you're using multiple vectors, you would have to go through them all to get an accurate count.
Nov 27, 2013 at 6:12pm
I think seven50 had the right answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <numeric>

int f(int x, std::vector<int>& v)
{
	return x + v.size();	
}
int main() 
{
	std::vector<std::vector<int>> vv{{1,2,3},{1,1},{4,1,2,3},{9},{1,2,3,4,5,6,7,8,9,0}};
	int count = 0;
	
	std::cout << std::accumulate(vv.begin(), vv.end(), count, f) << std::endl;
}

http://ideone.com/i7uEKX
Nov 29, 2013 at 8:49am
naraku9333 solution is very good i thing.
Thank you very much.

Jim.
Topic archived. No new replies allowed.