[英]Objective-C How can I use vector-pair?
I am new to Objective-C and I need to manipulate code same as below c++ code. 我是Objective-C的新手,我需要操作与下面的c ++代码相同的代码。
typedef long long ll;
vector< pair<ll, int> > v;
for(int i = 0; i < N; i++){
v.push_back( make_pair(token[i], N - i));
}
sort(v.begin(), v.end());
I need to use same code in Objective-C without loss of Object Oriented. 我需要在Objective-C中使用相同的代码而不会丢失面向对象。
Here is I tried Objective-C 这是我尝试过的Objective-C
#import <Foundation/Foundation.h>
typedef long long ll;
@interface Main:NSObject
-(void) main;
@end
@implementation Main
-(void) main{
struct Pair{
ll first;
int second;
};
NSMutableArray *v = [NSMutableArray new];
int N = 3;
NSMutableArray *vector = [NSMutableArray arrayWithCapacity:(int)N];
int i;
for(i=0;i<N;i++){
ll value = (ll)i;
printf("Value is: %lld",(ll)i);
[vector addObject:[NSNumber numberWithLongLong:(value)]];
}
for(int i = 0; i < N; i++){
struct Pair *p = malloc(sizeof(struct Pair));;
ll firstValue = [[vector objectAtIndex:i] longLongValue];
NSLog(@"\nFirst Value is: %lld",firstValue);
p->first = firstValue;
p->second = (int)N - i;
NSLog(@"\n\nAfter append it to P:%lld and Int is: %d \n\n",p->first,p->second);
NSValue *value = [NSValue valueWithBytes:p objCType:@encode(struct Pair)];
[v addObject:value];
}
[v sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
struct Pair *first = (__bridge struct Pair *)obj1;
struct Pair *second = (__bridge struct Pair *)obj2;
NSLog(@"%lld %d",first->first,first->second);
return first->first < second->second;
}];
for(int i = 0; i < N; i++){
//I need to print first and second from V
}
}
@end
int main() {
@autoreleasepool {
Main *main = [[Main alloc]init];
[main main];
}
return 0;
}
Issue: 问题:
I'm successfull upto NSLog(@"\\n\\nAfter append it to P:%lld and Int is: %d \\n\\n",p->first,p->second);
我成功通过
NSLog(@"\\n\\nAfter append it to P:%lld and Int is: %d \\n\\n",p->first,p->second);
But I can't get back same value from v
on here NSLog(@"%lld %d",first->first,first->second);
但我不能得到回相同的值
v
在这里NSLog(@"%lld %d",first->first,first->second);
. 。
So, How can I get back inserted struct
from v
? 那么,我如何从
v
取回插入的struct
?
Is it solve that C++ code to Objective - C with simple way instead of this? 用简单的方法代替这个解决Objective-C的C ++代码吗?
Output for above Objective-C code: 以上Objective-C代码的输出:
Value is: 0 Value is: 1 Value is: 2
值为:0值为:1值为:2
First Value is: 0
第一个值是:0
After append it to P:0 and Int is: 3 First Value is: 1
将其附加到P:0并且Int为:3第一个值为:1
After append it to P:1 and Int is: 2 First Value is: 2
将其附加到P:1并且Int是:2第一个值是:2
After append it to P:2 and Int is: 1
将其附加到P:2并且Int为:1
80501841873530129 0
80501841873530129 0
80501841873530129 0
80501841873530129 0
You are using value with bytes to construct an NSValue
from a pointer.. 您正在使用带字节的值来从指针构造
NSValue
。
You are sorting NSValue but casting them via bridge as a Pair *
.. 您正在对NSValue进行排序,但是通过桥接将它们作为一
Pair *
。
malloc
which is not necessary if you want the code to be exactly the same as the C++ version (each Pair
is stored in the vector as a copy.. not a pointer). malloc
在堆上进行分配,如果您希望代码与C ++版本完全相同(每个Pair
作为副本存储在向量中而不是指针),则不需要。 If you want to use pointers (to raw memory) then: 如果你想使用指针(到原始内存),那么:
#import <Foundation/Foundation.h>
typedef long long ll;
@interface Main:NSObject
-(void) main;
@end
@implementation Main
-(void) main{
struct Pair {
ll first;
int second;
};
NSMutableArray *v = [NSMutableArray new];
int N = 3;
NSMutableArray *vector = [NSMutableArray arrayWithCapacity:(int)N];
int i;
for(i=0;i<N;i++){
ll value = (ll)i;
printf("Value is: %lld",(ll)i);
[vector addObject:[NSNumber numberWithLongLong:(value)]];
}
for(int i = 0; i < N; i++){
struct Pair *p = malloc(sizeof(struct Pair));
ll firstValue = [[vector objectAtIndex:i] longLongValue];
NSLog(@"\nFirst Value is: %lld",firstValue);
p->first = firstValue;
p->second = (int)N - i;
NSLog(@"\n\nAfter append it to P:%lld and Int is: %d \n\n", p->first, p->second);
//Store pointer p as NSValue.
NSValue *value = [NSValue valueWithPointer:p];
[v addObject:value];
}
[v sortedArrayUsingComparator:^NSComparisonResult(NSValue *obj1, NSValue *obj2) {
//call `getValue` to get the pointer back..
struct Pair *first;
[obj1 getValue:&first];
struct Pair *second;
[obj2 getValue:&second];
NSLog(@"%lld %d", first->first, first->second);
return first->first < second->second;
}];
for(int i = 0; i < N; i++) {
NSValue *value = [v objectAtIndex:i];
//Call `getValue` to get the pointer back..
struct Pair *pair;
[value getValue:&pair];
NSLog(@"%lld %d", pair->first, pair->second);
free(pair); //clean up..
}
}
@end
int main() {
@autoreleasepool {
Main *main = [[Main alloc] init];
[main main];
}
return 0;
}
If you don't want to malloc and free stuff or deal with pointers (to raw memory).. then: 如果你不想使用malloc和free东西或处理指针(到原始内存)..那么:
#import <Foundation/Foundation.h>
typedef long long ll;
@interface Main:NSObject
-(void) main;
@end
@implementation Main
-(void) main{
struct Pair {
ll first;
int second;
};
NSMutableArray *v = [NSMutableArray new];
int N = 3;
NSMutableArray *vector = [NSMutableArray arrayWithCapacity:(int)N];
int i;
for(i=0;i<N;i++){
ll value = (ll)i;
printf("Value is: %lld",(ll)i);
[vector addObject:[NSNumber numberWithLongLong:(value)]];
}
for(int i = 0; i < N; i++){
struct Pair p;
ll firstValue = [[vector objectAtIndex:i] longLongValue];
NSLog(@"\nFirst Value is: %lld",firstValue);
p.first = firstValue;
p.second = (int)N - i;
NSLog(@"\n\nAfter append it to P:%lld and Int is: %d \n\n", p.first, p.second);
//Encode p as a struct into an `NSValue`
NSValue *value = [NSValue value:&p withObjCType:@encode(struct Pair)];
[v addObject:value];
}
[v sortedArrayUsingComparator:^NSComparisonResult(NSValue *obj1, NSValue *obj2) {
//Get each pair back
struct Pair first;
[obj1 getValue:&first];
struct Pair second;
[obj2 getValue:&second];
NSLog(@"%lld %d",first.first, first.second);
return first.first < second.second;
}];
for(int i = 0; i < N; i++) {
NSValue *value = [v objectAtIndex:i];
//print each pair..
struct Pair pair;
[value getValue:&pair];
NSLog(@"%lld %d", pair.first,pair.second);
}
}
@end
int main() {
@autoreleasepool {
Main *main = [[Main alloc] init];
[main main];
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.