[obj-c] Object-C Stack 容器

本文介绍了一种在Objective-C中手动实现栈容器的方法。由于Objective-C标准库中并未提供栈容器,作者自行设计并实现了栈的基本操作,包括push、pop和clear等,并通过NSMutableArray来模拟栈的行为。

由于Object-C中没有提供Stack容器,因此自己实践了一个简单的stack容器

#import <Foundation/Foundation.h>

@interface NSStack : NSObject {
	NSMutableArray* m_array;
	int count;
}

- (void)push:(id)anObject;
- (id)pop;
- (void)clear;

@property (nonatomic, readonly) int count;

@end

#import "NSStack.h"

@implementation NSStack

@synthesize count;

- (id)init
{
	if( self=[super init] )
	{
		m_array = [[NSMutableArray alloc] init];
		count = 0;
	}
	return self;
}

- (void)dealloc {
	[m_array release];
	[self dealloc];
    [super dealloc];
}

- (void)push:(id)anObject
{
	[m_array addObject:anObject];
	count = m_array.count;
}
- (id)pop
{
    id obj = nil;
    if(m_array.count > 0)
    {
        obj = [[[m_array lastObject]retain]autorelease];
        [m_array removeLastObject];
        count = m_array.count;
    }
    return obj;
}

- (void)clear
{
	[m_array removeAllObjects];
        count = 0;
}

@end






符号链接 ./Steam++ 已存在 Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'BD.Common.VersionTracking2' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'e_sqlite3' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: /opt/steam++/assemblies/e_sqlite3.so: cannot open shared object file: No such file or directory /opt/steam++/dotnet/shared/Microsoft.NETCore.App/8.0.0/e_sqlite3.so: cannot open shared object file: No such file or directory /opt/steam++/assemblies/libe_sqlite3.so: cannot open shared object file: No such file or directory /opt/steam++/dotnet/shared/Microsoft.NETCore.App/8.0.0/libe_sqlite3.so: cannot open shared object file: No such file or directory /opt/steam++/assemblies/e_sqlite3: cannot open shared object file: No such file or directory /opt/steam++/dotnet/shared/Microsoft.NETCore.App/8.0.0/e_sqlite3: cannot open shared object file: No such file or directory /opt/steam++/assemblies/libe_sqlite3: cannot open shared object file: No such file or directory /opt/steam++/dotnet/shared/Microsoft.NETCore.App/8.0.0/libe_sqlite3: cannot open shared object file: No such file or directory at System.Runtime.InteropServices.NativeLibrary.LoadLibraryByName(String libraryName, Assembly assembly, Nullable`1 searchPath, Boolean throwOnError) at BD.WTTS.GlobalDllImportResolver.Delegate(String libraryName, Assembly assembly, Nullable`1 searchPath) at System.Runtime.InteropServices.NativeLibrary.LoadLibraryCallbackStub(String libraryName, Assembly assembly, Boolean hasDllImportSearchPathFlags, UInt32 dllImportSearchPathFlags) at SQLitePCL.SQLite3Provider_e_sqlite3.NativeMethods.sqlite3_libversion_number() at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number() at SQLitePCL.raw.SetProvider(ISQLite3Provider imp) at SQLitePCL.Batteries_V2.Init() at SQLite.SQLiteConnection..cctor() --- End of inner exception stack trace --- at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks) at BD.Common.Repositories.Abstractions.Repository.get_ConnectionSync() at BD.Common.Services.Implementation.PreferencesPlatformServiceImpl..ctor() at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at BD.Common.Services.IPreferencesPlatformService.get_Instance() at BD.Common.VersionTracking2.InitVersionTracking() at BD.Common.VersionTracking2..cctor() --- End of inner exception stack trace --- at BD.Common.VersionTracking2.Track() at BD.WTTS.UI.Views.Windows.AppSplashScreen.<>c.<<FluentAvalonia-UI-Windowing-IApplicationSplashScreen-RunTasks>b__14_0>d.MoveNext() --- End of stack trace from previous location --- at FluentAvalonia.UI.Windowing.SplashScreenContext.RunJobs() at FluentAvalonia.UI.Windowing.AppWindow.OnOpened(EventArgs e) at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Avalonia.Threading.SendOrPostCallbackDispatcherOperation.InvokeCore() at Avalonia.Threading.DispatcherOperation.Execute() at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job) at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean fromExplicitBackgroundProcessingCallback) at Avalonia.Threading.Dispatcher.OnOSTimer() at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken) at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl) at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime2.Start(String[] args) at BD.WTTS.Program.StartUIApplication() at BD.WTTS.Startup.<ConfigureCommands>b__0_12() at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.InvokeAsync(InvocationContext context) at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<<UseHelp>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass19_0.<<UseTypoCorrections>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__18_0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseDirective>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__5_0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<<UseExceptionHandler>b__0>d.MoveNext()
最新发布
11-26
03-13
### Python 中 `object` 的基本概念 在Python中,`object` 是所有类默认继承的基础类。任何自定义类如果没有显式地指明父类,默认都会隐式地继承自 `object` 类[^1]。 ```python class MyClass(object): # 显式继承 object pass class MyOtherClass: # 隐式继承 object (Python 3.x 默认行为) pass ``` 创建的对象实例可以通过比较操作符来判断是否相等: ```python obj1 = MyClass() obj2 = MyClass() print("Is obj1 equal to obj2:" + str(obj1 == obj2)) ``` 这段代码展示了如何通过 `==` 进行两个对象之间的比较运算。 ### C++ 编译与链接过程中的 `.obj` 文件 对于C/C++项目,在编译过程中会产生中间目标文件(`.obj` 或者 `.o`),这些文件包含了机器码以及符号表信息。当涉及到跨语言调用时,比如C++ 调用 C 函数,则可能需要用到 `extern "C"` 来防止名称修饰(name mangling),以便于链接器能够正确找到对应的函数实现[^2]。 ### Makefile 构建系统里的 `obj-*` 变量 在Linux内核或其他复杂项目的Makefiles里经常可以看到像 `obj-y`, `obj-m`, 和 `obj-n` 这样的变量用于控制哪些源文件应该被编入最终产物之中。具体来说: - 使用 `obj-y := func.o` 表示将 `func.o` 直接加入到内核镜像; - 如果希望某个模块仅作为可加载模块存在而不内置进内核,则可以用 `obj-m += module.o`; - 若要阻止特定文件参与构建流程,那么就设置 `obj-n := unwanted.o`. 此外还有更细粒度的方式去管理依赖关系,例如利用 `-objs` 后缀指定某模块的具体构成部分[^3]. ### C++ 模板类概述 模板类是C++提供的一种泛型编程特性,允许开发者设计出能处理不同类型的数据结构或算法而无需重复书写相似逻辑的代码片段。这不仅增强了程序的功能抽象层次,同时也促进了软件组件间的解耦合程度。例如下面展示了一个简单的栈(stack)容器模板定义[^4]: ```cpp template<typename T> class Stack { public: void push(const T& item); T pop(); private: std::vector<T> elements; }; ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值