我是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());
我需要在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;
}
问题:
我成功到达NSLog(@"\n\nAfter append it to P:%lld and Int is: %d \n\n",p->first,p->second);
但我无法从v
NSLog(@"%lld %d",first->first,first->second);
取回相同的值struct
。
那么,如何从v
export const ex: any;
是否用简单的方法解决了Objective-C的C ++代码而不是这个?
以上Objective-C代码的输出:
值为:0值为:1值为:2
第一个值是:0
将其追加到P:0并且Int为:3第一个值为:1
将其追加到P:1并且Int为:2第一个值为:2
将其附加到P:2并且Int为:1
80501841873530129 0
80501841873530129 0
答案 0 :(得分:1)
您正在使用带字节的值从指针构造NSValue
..
您正在对NSValue进行排序,但是通过网桥将其作为Pair *
进行投放..
malloc
在堆上进行分配,如果您希望代码与C ++版本完全相同(每个Pair
作为副本存储在向量中,则不需要;}指针)。如果你想使用指针(到原始内存),那么:
#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;
}
如果你不想使用malloc和免费的东西或处理指针(原始内存)..那么:
#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;
}