天道酬勤,学无止境

subscript-operator

is int[pointer-to-array] in the C++ - standard? [duplicate]

问题 这个问题在这里已经有了答案: 对于数组,为什么a[5] == 5[a]? (19 个回答) 为什么 x[y] == y[x] 在 c++ 中? [重复] (3个答案) 7年前关闭。 据我所知,可以编写以下代码: char *a = new char[50]; for (int i = 0; i < 50; ++i) { i[a] = '5'; } 它编译。 有用。 它的作用与 char *a = new char[50]; for (int i = 0; i < 50; ++i) { a[i] = '5'; } 难道仅仅是因为: a[b]默认实现为宏*(a + b)并且两个代码示例都有效的事实只是意外/编译器特定的它在某处标准化,并且此类算法的结果在每个平台上都应该相同 假设加法应该是可交换的是合理的,但是如果我们以这种方式实现operator[] ,我们已经使其他东西具有可交换性,这可能不是我们想要的。 有趣的事实是没有pointer[pointer]操作符,所以operator[]不是宏。 我知道这很糟糕。 我知道这会让阅读代码的人感到困惑。 但我想知道这是否只是一个意外,它不会在独角兽有七条腿且左脸颊上有角的遥远土地上起作用。 回答1 C++ 标准,第 8.3.4 节,注释 7(第 185 页)(强调我的)。 除了已为类 (13.5.5) 声明的情况外,下标运算符[

2022-04-29 08:08:15    分类:技术分享    c++   arrays   pointers   syntax   subscript-operator

Bug in Swift array subscript indexing?

I've isolated some Swift code from my project that can be pasted into a Playground. It produces an error "Could not find an overload for '+' that accepts the supplied arguments" both in normal Xcode editing and the Playground. The error refers to the last (non-trivial) line. import UIKit let points = 40 let max = points-1 let L = 10.0 let Deltat = 0.01 let Deltax = L/Double(points) var a = [Double](count: points, repeatedValue: 0.0) var b = [Double](count: points, repeatedValue: 0.0) var c = [Double](count: points, repeatedValue: 0.0) for i in 1..<max-1 { //let iPlus1 = i+1 //let temp = 0.5

2022-04-16 14:34:17    分类:问答    ios   arrays   swift   swift-playground   subscript-operator

is int[pointer-to-array] in the C++ - standard? [duplicate]

This question already has answers here: With arrays, why is it the case that a[5] == 5[a]? (19 answers) Why does x[y] == y[x] in c++? [duplicate] (3 answers) Closed 7 years ago. As I have learned, one can write the following code: char *a = new char[50]; for (int i = 0; i < 50; ++i) { i[a] = '5'; } It compiles. It works. It does exactly the same as char *a = new char[50]; for (int i = 0; i < 50; ++i) { a[i] = '5'; } Is it just because: a[b] is implemented as a macro *(a + b) by default and the fact that both code samples are valid is just an accident/compiler specific it's standardized

2022-03-25 11:06:13    分类:问答    c++   arrays   pointers   syntax   subscript-operator

Array Subscription: returning Reference vs proxy class method

While searching for methods for overloading Subscript('[]') operator for template class, I came across two different techniques. First Technique: Overloading the operator [] returning pointer to the container directly, which will allow both reading value and assigning value. A sample implementation of this technique: template <class T> class X { int _size; T *container; public: X(int sz) { _size=sz; container=new T[sz](); } ~X() { } T& operator [](int indx) { return container[indx]; } }; With main() as: X<int> sample(100); cout<<sample[9]<<endl; sample[9]=9; cout<<sample[9]<<endl; Output: 0 9

2022-03-16 09:32:14    分类:问答    c++   operator-overloading   subscript-operator

指针是否支持“数组样式索引”?(Do pointers support "array style indexing"?)

问题 (自答Q&A-此问题不断弹出) 我假设读者知道指针算法是如何工作的。 int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; 老师/C 书籍一直告诉我我不应该像上面的例子那样使用*(ptr + i) ,因为“指针支持数组样式索引”,我应该使用ptr[i] = value; 反而。 没有争论 - 更容易阅读。 但是查看 C 标准,我发现没有什么叫做“数组样式索引”。 事实上,操作符[]并不期望任何一个操作数是一个数组,而是一个指针或一个整数! 6.5.2.1 数组下标约束其中一个表达式的类型应为“指向完整对象类型的指针”,另一个表达式应为整数类型,结果类型为“类型”。 为什么数组下标运算符不期望数组? 标准错了吗? 我的老师/C 书糊涂了吗? 回答1 出于可读性原因,您确实应该在*(ptr + i)上使用ptr[i] 。 但除此之外,严格来说, []运算符实际上从未与数组操作数一起使用。 数组在表达式中使用时,总是“衰减”为指向第一个元素的指针(有一些例外)。 C17 6.3.2.1/3,强调我的: 除非它是 sizeof 运算符的操作数,或者一元 & 运算符,或者是用于初始化数组的字符串文字,否则类型为“array of type”的表达式将转换为类型为“pointer”的表达式键入 ''

2021-12-05 02:26:14    分类:技术分享    c   arrays   pointer-arithmetic   subscript-operator   array-indexing

Stack overflow when defining subscript on CKRecord in Swift

This question asks whether one can use subscripting with CKRecord in Swift. While I already knew how to do what the questioner wanted, every permutation of it gives me a stack overflow: subscript(key: String) -> CKRecordValue? { get { return objectForKey(key) as CKRecordValue? } set { setObject(newValue, forKey: key) } } The stack overflow occurs in the getter. (I've never tried the setter, so it may occur there, too.) I've tried implementing with objectForKey:, objectForKeyedSubscript:, and valueForKey:. All produce the same result: a stack overflow. This is very strange, since CKRecord is

2021-11-24 22:45:11    分类:问答    swift   cloudkit   subscript-operator   ckrecord

Do pointers support "array style indexing"?

(Self-answered Q&A - this matter keeps popping up) I assume that the reader is aware of how pointer arithmetic works. int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; Teachers/C books keep telling me I shouldn't use *(ptr + i) like in the above example, because "pointers support array style indexing" and I should be using ptr[i] = value; instead. No argument there - much easier to read. But looking through the C standard, I find nothing called "array style indexing". In fact, the operator [] is not expecting either operand to be an array, but instead a pointer or an integer! 6.5.2

2021-11-14 10:43:21    分类:问答    c   arrays   pointer-arithmetic   subscript-operator   array-indexing

在左值和右值情况下重载下标运算符“[]”(Overloading the subscript operator “[ ]” in the l-value and r-value cases)

问题 我在我的类Interval 中重载了 [] 运算符以返回分钟或秒。 但我不确定如何使用 [] 运算符将值分配给分钟或秒。 例如:我可以使用这个语句cout << a[1] << "min and " << a[0] << "sec" << endl; 但我想重载 [] 运算符,以便我什至可以使用将值分配给分钟或秒 a[1] = 5; a[0] = 10; 我的代码: #include <iostream> using namespace std; class Interval { public: long minutes; long seconds; Interval(long m, long s) { minutes = m + s / 60; seconds = s % 60; } void Print() const { cout << minutes << ':' << seconds << endl; } long operator[](int index) const { if(index == 0) return seconds; return minutes; } }; int main(void) { Interval a(5, 75); a.Print(); cout << endl; cout << a[1] << "min and " << a[0]

2021-10-07 22:37:14    分类:技术分享    c++   operator-overloading   subscript-operator