Objective-C 如何使用向量对?

Objective-C How can I use vector-pair?

本文关键字:向量 何使用 Objective-C      更新时间:2023-10-16

我是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(@"nnAfter append it to P:%lld and Int is: %d nn",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(@"nnAfter append it to P:%lld and Int is: %d nn",p->first,p->second);但我无法从这里的v那里获得相同的价值NSLog(@"%lld %d",first->first,first->second);.

那么,如何从v中取回插入struct

它是否用简单的方法解决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

  1. 您正在使用带有字节的值从指针构造NSValue

  2. 您正在对 NSValue 进行排序,但通过桥将它们作为Pair *进行转换......

  3. 您可以通过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(@"nnAfter append it to P:%lld and Int is: %d nn", 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;
}

如果你不想恶意和免费的东西或处理指针(原始内存(..那么:

#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(@"nnAfter append it to P:%lld and Int is: %d nn", 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;
}