subscript

overloading assignment operator With subscript operator

有些话、适合烂在心里 提交于 2019-12-01 09:03:10
问题 I overloaded both subscript operator and assignment operator and I am trying to get right value to assignment operator example Array x; x[0]=5; by overloading subscript operator i can get value 0 but when i overload assignment operator it does the assignment but it doesn't use my overloaded function because vaiable 2 should have value 5. class Array { public: int *ptr; int one,two; Array(int arr[]) { ptr=arr; } int &operator[](int index) { one=index; return ptr[index]; } int & operator=(int x

Is it possible to add “keyed-subscripting” to Class objects?

走远了吗. 提交于 2019-12-01 06:51:44
In the vein of... @implementation MyClass - (id) objectForKeyedSubscript:(id)k { return [self something:k]; } Is it also possible to "subscript" Class objects? I too, am about to find out, with you.. but thought I would post this question as I tested it out, myself... + (id) objectForKeyedSubscript:(id)k { return [self.shared something:k]; } And alas.. it is not... id x = MyClass[@"document"]; error: unexpected interface name 'MyClass': expected expression But why, Daddy? Class ' sure get the short end of NSObject 's stick, if you ask me . Alex Gray Josh Caswell's comment pointed out the

Use a vector to index a matrix without linear index

你说的曾经没有我的故事 提交于 2019-12-01 05:33:18
G'day, I'm trying to find a way to use a vector of [x,y] points to index from a large matrix in MATLAB. Usually, I would convert the subscript points to the linear index of the matrix.(for eg. Use a vector as an index to a matrix ) However, the matrix is 4-dimensional, and I want to take all of the elements of the 3rd and 4th dimensions that have the same 1st and 2nd dimension. Let me hopefully demonstrate with an example: Matrix = nan(4,4,2,2); % where the dimensions are (x,y,depth,time) Matrix(1,2,:,:) = 999; % note that this value could change in depth (3rd dim) and time (4th time) Matrix(3

Use a vector to index a matrix without linear index

烈酒焚心 提交于 2019-12-01 03:35:35
问题 G'day, I'm trying to find a way to use a vector of [x,y] points to index from a large matrix in MATLAB. Usually, I would convert the subscript points to the linear index of the matrix.(for eg. Use a vector as an index to a matrix) However, the matrix is 4-dimensional, and I want to take all of the elements of the 3rd and 4th dimensions that have the same 1st and 2nd dimension. Let me hopefully demonstrate with an example: Matrix = nan(4,4,2,2); % where the dimensions are (x,y,depth,time)

How to print subscripts/superscripts on a CLI?

◇◆丶佛笑我妖孽 提交于 2019-11-30 20:57:50
I'm writing a piece of code which deals with math variables and indices, and I'd need to print subscripts and superscripts on a CLI, is there a (possibly cross-platform) way to do that? I'm working in vanilla C++. Note: I'd like this to be cross-platform, but since from the first answers this doesn't seem to be possible I'm working under MacOS and Ubuntu Linux (so bash). Thank you Since most CLIs are really only terminals (pretty dumb ones mostly but sometimes with color), the only cross-platform way I've ever done this is by allocating muliple physical lines per virtual line, such as: 2 f(x)

Swift讲解专题十三——下标访问

不打扰是莪最后的温柔 提交于 2019-11-30 20:48:34
Swift讲解专题十三——下标访问 一、引言 在以前的博客中,讨论过在Objective-C中,通过下标的方式访问自定义数据模型的方法。Objective-C中主要是通过实现一系列方法来使自定义的数据类型支持下标的访问方式,博客地址如下: 在Objective-C中使用下标访问自定义数据模型: http://my.oschina.net/u/2340880/blog/632294 。 Swift中的Array,Dictionary类型可以通过下标或者键值的方式来进行数据的访问,实际上在Swift的语法中,下标可以定义在类、结构体、枚举中。开发者可以通过下标的方式来对属性进行访问而不用使用专门的存取方法。并且定义的下标不限于一维,开发者可以定义多维的下标来满足需求。 二、下标的语法结构 下标使用subscript来定义,其有些类似于方法,参数和返回值分别作为下标入参和通过下标所取的值。但是在subscript实现部分,又十分类似于计算属性,其需要实现一个get块和可选实现一个set块,get块用于使用下标取值,set块用于使用下标设置值,因此,subscript结构更像是计算属性和方法的混合体,示例如下: class MyClass { var array=[1,1,1,1,1] subscript(param1:Int)->Int{ set{ array[param1] =

c++ two versions of overloading subscript operator

南笙酒味 提交于 2019-11-30 19:06:31
My question is about the difference between: const T& operator[](const int nIndex) const; and: T& operator[](const int nIndex); Why would I need both of them defined in a class, what's the purpose? Wouldn't the latter suffice? A member function declaration with const at the end of it allows that function to be called even on a const object. This only makes sense for member functions that don't modify the state of the object. Let's say the class you have that overloads these operators is called X . Presumably it behaves a bit like a container, giving access to the elements it contains through

Subscript: access my dictionary values with a String enumeration

↘锁芯ラ 提交于 2019-11-30 17:03:55
I want to do something like that: access my dictionary values with a String enumeration. I am trying to overload the subscript of the dictionary but without success. Accessing the dictionary: let district = address[JsonKeys.district] where JsonKeys is: enum JsonKeys: String { case key1 case key2 case key... } and my subscript overload is as follow: extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject { subscript(index: FOJsonKeys) -> AnyObject { get { return self[ index.rawValue] as! AnyObject } } } I get the following message: **Cannot subscript a value of type

c++ two versions of overloading subscript operator

柔情痞子 提交于 2019-11-30 03:49:07
问题 My question is about the difference between: const T& operator[](const int nIndex) const; and: T& operator[](const int nIndex); Why would I need both of them defined in a class, what's the purpose? Wouldn't the latter suffice? 回答1: A member function declaration with const at the end of it allows that function to be called even on a const object. This only makes sense for member functions that don't modify the state of the object. Let's say the class you have that overloads these operators is

Subscript: access my dictionary values with a String enumeration

半城伤御伤魂 提交于 2019-11-29 23:50:45
问题 I want to do something like that: access my dictionary values with a String enumeration. I am trying to overload the subscript of the dictionary but without success. Accessing the dictionary: let district = address[JsonKeys.district] where JsonKeys is: enum JsonKeys: String { case key1 case key2 case key... } and my subscript overload is as follow: extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject { subscript(index: FOJsonKeys) -> AnyObject { get { return self[ index