简体   繁体   English

Objective-C如何使用矢量对?

[英]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

  1. You are using value with bytes to construct an NSValue from a pointer.. 您正在使用带字节的值来从指针构造NSValue

  2. You are sorting NSValue but casting them via bridge as a Pair * .. 您正在对NSValue进行排序,但是通过桥接将它们作为一Pair *

  3. You allocate on the heap via 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.

相关问题 如何在XCode中使用带有Objective-C的C ++ - How can I use C++ with Objective-C in XCode 如何在带有xcode的Objective-C项目中使用自定义类型(C ++)? - How can I use my custom types (C++) in an objective-c project with xcode? 如何在C ++中构建和使用vector(map(pair(struct)))? - How can I build and use a vector(map(pair(struct))) in C++? 如何在Objective-C中使用MurmurHash 64? - How to use MurmurHash 64 in Objective-C? 如何在Mac OS上的C / C ++ / Objective-C中找出SystemUIServer进程的PID? - How can I find out the PID of the SystemUIServer process in C/C++/Objective-C on Mac OS? 如何在XCode 4.2中一起使用c ++和Objective-c - How to use c++ and objective-c together in XCode 4.2 如何链接和使用 Objective-C Xcode 项目中的 LLVM 库? - How do I link and use LLVM libraries from an Objective-C Xcode project? 如何确定运行时void *指针是指向Objective-C对象还是C ++对象 - How can I determine whether a void * pointer points to a objective-c object or a c++ object at runtime 将 C++ 与 Objective-C 一起使用,如何解决“冲突声明 'typedef int BOOL'”? - Using C++ with Objective-C, How can I fix “Conflicting declaration 'typedef int BOOL'”? 如何使用 Visual Studio C/C++ 编译器 (cl.exe) 来预处理我的 Objective-C 代码? - How can I use the Visual Studio C/C++ Compiler (cl.exe) to pre-process my objective-C code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM