没有运算符“="匹配这些操作数 [英] No operator "=" matches these operands
问题描述
所以这一定是很愚蠢的事情,但我收到了这段代码的错误.
So this must be something really silly, but I'm getting an error with this code.
可能出了什么问题,操作数 <, > 也不起作用.使用向量的方式不同吗?当我尝试 y.at(1) = 10;它说表达式必须具有类类型...?
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;
}
对于上下文,我正在尝试测试运行时间.起初使用 std::array ,但似乎不适用于大数组大小,所以我决定尝试向量.
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
std::vector<int> y[10];
声明一个包含 10 个空向量的数组.要拥有一个包含 10 个元素的向量,您需要
declares an array of 10 empty vectors. To have a vector of 10 elements you need
std::vector<int> y(10);
相反.
你并不是唯一一个认为犯这个错误时的错误信息有点神秘的人......不幸的是,这是一个缺乏 C++ 的领域(现在不确定,但我记得有一些公司以此为生只是破译来自 VC++ 的 C++ 错误消息).
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++).
这篇关于没有运算符“="匹配这些操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!