Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337091
  • 博文数量: 97
  • 博客积分: 2130
  • 博客等级: 大尉
  • 技术积分: 1800
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 11:51
文章分类
文章存档

2013年(57)

2012年(40)

我的朋友

分类: C/C++

2012-10-29 13:00:29

  为了更好的使STL适合.net开发,Visual C++产品组,2005版的Visual C++中重新设计了STL,并命名为STL.NET,从Beta1版本的产品中开始提供。在STL.NET的设计中,STL的实现使用了CLI泛型和C++模版机制。2005版本的C++将加入C++/CLI动态编程的支持,应当会成为最能够满足程序员设计的语言。

  总共有三个容器库可供程序员用于操作CLI类型,这三个容器库建于三种类型参数化模型之上。

  原先元素类型存储的Systems::Collection 库是基于CLI类型中的对象基类来实现的。如下的 ArrayList实现了IList接口。它代表类型对象的数组,在本例中用于控制String类型的元素。(这里采用版本2的语法来实现)

void objectCollection()
{
 using namespace System::Collections;
 
ArrayList ^as = gcnew ArrayList;
 
as->Add( "Pooh" ); as->Add( "Piglet" );
 
as->Add( "Eeyore" ); as->Add( "Rabbit" );
 
as->Sort();
 
Console::WriteLine( "ArrayList holds {0} elements: ",as->Count );
 
for ( int i = 0; i < as->Count; i++ )
  
Console::WriteLine( as[ i ] );
  
int index = as->IndexOf( "Pooh" );
  
if ( index != -1 )
  
{
   //需要一个清晰地
downcast
   
String^ item = safe_cast( as[ index ]);
   
as->RemoveAt( index );
  
}
  
as->Remove( "Rabbit" );
  
Console::WriteLine( "\nArrayList holds {0} elements: ",as->Count );
  
IEnumerator^ is = as->GetEnumerator();
  
while ( is->MoveNext() )
   
Console::WriteLine( is->Current );
}

  现在我们引入了一个基于CLI泛型机制的新的容器库。可以在System::Collections::Generic 命名空间中找到。这是在Visual Studio 2005 Beta1中的实现,在最终的发布版当中可能会有所改变。Collection 是一个具体的泛型基类,用户们可以从其中派生自己特化的容器类。下面的样例与上面的例子作用相同,只是使用了新的容器库,

  void genericCollection()
  
{
    
using namespaces System::Collections::Generic;
    
Collection^cols = gcnew Collection;
    
cols->Add("Pooh");cols->Add("Piglet");
    
cols->Add("Eeyore");cols->Add("Rabbit");
    //没有与Collection关联的Sort方法

    Console::WriteLine("Collection holds {0} elements:",cols->Count);
    
for (int i=0; iCount;i++)
      
Console::WriteLine(cols[i]);
      
int index = cols->IndexOf("Pooh");
      
if(index!=-1)
      
{
        //不需要
downcast……
        
String^item = cols[index];
        
cols->RemoveAt(index);
      
}
      
cols->Remove("Rabbit");
      
Console::WriteLine("\nCollection holds {0} elements:",cols->Count);
      
IEnumerator^is = cols->GetEnumerator();
      
while(is->MoveNext())
        
Console::WriteLine(is->Current);
  }

  STL.NET提供了一个与以往设计风格迥异的类型参数化模型,我们将在下个话题中谈到。下面是String容器的实现。

未完待续,希望关注我的微博,也可以在下载文档。我也是在这里看到的,分享给大家。

阅读(1260) | 评论(0) | 转发(0) |
0

上一篇:PS快捷键大全

下一篇:误删资料恢复

给主人留下些什么吧!~~