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

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? 

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
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
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.

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
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
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.
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
naraku9333 solution is very good i thing.
Thank you very much.

Jim.
Topic archived. No new replies allowed.