So this must be something really silly, but I'm getting an error with this code.
What could be going wrong, the operands <, > also don't work. Does one use vectors differently? When I try y.at(1) = 10; it says expression must have class type...?
#include "stdafx.h"
#include <iostream>
#include "time.h"
#include <vector>
int main()
{
using namespace std;
const long long l = 100000;
vector <int> y[l];
long long x[l];
y[0] = 10; // Test statement results in Error.
//for (long i = 0;i < l;i++) {
// y.at(i) = i;//rand() % 100;
// x[i] = rand() % 100;
//}
clock_t t = clock();
for (long long i = 0;i < l;i++) {
long long r;
r = y[i] ^ ((x[i]^y[i]) & -(x[i] < y[i]));
/*if (x[i] < y[i]) {
r = x[i];
}
else {
r = y[i];
}*/
}
t = clock() - t;
printf("It took %d ms ", t);
return 0;
}
For context I'm trying to test for run times. Was using std::array at first, but it seems like that doesn't work with large array sizes, so I decided to try out vectors.
Used http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm as a reference, but it seems like although I'm doing the exact same thing, something is not working.
This is a quite common typo, writing
declares an array of 10 empty vectors. To have a vector of 10 elements you need
instead.
You're not alone in thinking that the error message when making this mistake is somewhat cryptic... this is an area in which unfortunately C++ is lacking (not sure now, but I remember that there were companies making a living on just deciphering C++ error messages from VC++).
You've declared an array of vectors, not a vector. You want
vector <int> y(l);