We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,063 Members — Technology Publication meets Social Media

# Vector Dimensions

Hello,

I'm just wondering whether or not, it is possible to get the vector Dimensions without them being set?

Basically, I'm converting some matlab code and there's a function size(VECTOR) which get's the dimentions of the vector being passed.

For example:

``````#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {

vector<vector<int> > items;

vector<int> theItems(20);

for(unsigned i=0; (i < 20); i++)
{
theItems.push_back(i);
items.push_back(theItems);
}
}
``````

I need to determine the dimensions of items. Can I use something like capacity? Or is there a STL function like in matlab where you have size.

3
Contributors
3
Replies
4 Hours
Discussion Span
9 Months Ago
Last Updated
4
Views
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Skill Endorsements: 16

If you want to know the size of a vector you would use `items.size();`. If you want to know the max amount of elements that you can have in the vector before it is resized than you would use `items.capacity();`.

NathanOliver
Posting Virtuoso
1,515 posts since Apr 2009
Reputation Points: 281
Skill Endorsements: 3

Basically, I am trying to convert this code (matlab):

``````function f = strip(blocks, sumthresh, zerocrossthresh)

% This function removes leading and trailing blocks that do
% not contain sufficient energy or frequency to warrent consideration.
% Total energy is measured by summing the entire vector.
% Frequency is measured by counting the number of times 0 is crossed.
% The parameters sumthresh and zerocrossthrech are the thresholds,
% averaged across each sample, above which consideration is warrented.

% A good sumthresh would be 0.035
% A good zerocrossthresh would be 0.060

len = length(blocks);
n = sum(size(blocks)) - len;
min = n+1;
max = 0;
sumthreshtotal = len * sumthresh;
zerocrossthreshtotal = len * zerocrossthresh;
for i = 1:n
currsum = sum(abs(blocks(i,1:len)));
currzerocross = zerocross(blocks(i,1:len));
if or((currsum > sumthreshtotal),(currzerocross > zerocrossthreshtotal))
if i < min
min = i;
end
if i > max;
max = i;
end
end
end

% Uncomment these lines to see the min and max selected
% max
% min

if max > min
f = blocks(min:max,1:len);
else
f = zeros(0,0);
end
``````

But for some reason, I don't know why they are using this:

``````n = sum(size(blocks)) - len;
``````

Because, surely, I can just interate through the 2D vector, and then check to see if the blocks contain the criteral for energy/frequency..

Any ideas? :)

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Skill Endorsements: 16

Depends on how blocks is ultimately defined.
Depends on what length(blocks) returns.
Depends on what size(blocks) returns.
Depends on what sum(size(blocks)) returns.

There are a lot of possibilities on each of the above. Unless there's a Matlab user that can answer these questions, we'd be just guessing.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421