GDAL CplStringList 学习 理解 使用

开头

CPLStringList是一个基于C++的字符串列表类,它可以用于存储任意数量的字符串,并支持添加、插入、删除、排序等常见操作。该类由GDAL提供,用于在GDAL中管理字符串列表,例如GDAL数据集的元数据、选项等

CPLStringList     

官方文档: GDAL:CPLStringList 类参考

AddNameValue()

AddNameValue ( const char * pszKey,  const char * pszValue  )

将name=value条目添加到列表中

参数

pszKey 要添加的键名称

psz值 要添加的键值

oList.AddNameValue("name", "John Doe");

printf("%s\n", oList[0]);

name=John Doe

AddString

AddString ( const char * pszNewString )

添加字符串

参数:

pszNewString 要添加到列表中的字符串

CPLStringList list;

// add some strings to the list
list.AddString("string1");
list.AddString("string2");
list.AddString("string3");

// print the contents of the list
printf("The contents of the list are:\n");
for (int i = 0; i < list.Count(); i++)
{
    printf("%s\n", list[i]);
}

The contents of the list are:
string1
string2
string3

 扩展

AddString也能添加键值对,两者效果一样

oList.AddNameValue("name", "John Doe");

oList.AddString("name=John Doe");

AddStringDirectly()

AddStringDirectly(char * pszNewString)

参数:

pszNewString 要添加到列表中的字符串

char pszString1[] = "foo";
char pszString2[] = "bar";
CPLStringList oList;

// 将字符串1添加到列表
oList.AddStringDirectly(pszString1);

// 将字符串2添加到列表
oList.AddStringDirectly(pszString2);

// 打印列表
for (int i = 0; i < oList.size(); i++)
{
    printf("String %d: %s\n", i, oList[i]);
}

String 0: foo
String 1: bar

对比

AddStringDirectly() 接受一个指向字符串的指针,它会将这个指针添加到列表中。换句话说,它不会复制字符串,而是直接使用原来的指针,这就意味着这个指针指向的字符串必须在调用 AddStringDirectly() 的对象存在期间一直有效。这个方法通常用于将字符串从其他数据结构添加到 CPLStringList 中。

AddString() 接受一个字符串作为参数,它会创建这个字符串的一个副本,并将副本添加到列表中。这意味着它是安全的,因为即使原始字符串的生存期已经结束,副本仍然存在于列表中。这个方法通常用于创建新字符串并将其添加到 CPLStringList 中。

Assgin()

Assign ( char ** papszListIn,int bTakeOwnership = TRUE )

分配一个字符串列表

参数:papszlistin要使用的以NULL结束的字符串列表。

CPLStringList oList1;
oList1.AddString("foo");
oList1.AddString("bar");
oList1.AddString("dog");

CPLStringList oList2;
oList2.AddString("hello");
oList2.AddString("world");

oList1.Assign(oList2);
// 现在oList1中的元素是"hello"和"world",而不是"foo" "bar" "dog"

最终olist1保留

Count()

返回列表中字符串的计数,如果为空则为零

FetchBool

FetchBool ( const char * pszKey,bool bDefault ) const

检查布尔键值

参数:

pszKey 要查找的键值(不区分大小写)。

bDefault  如果根本找不到键,则返回的值。

返回:  True或 False

检查布尔键值。在“Name= Value”对的CPLStringList中,查看是否有给定名称的键,以及它是否可以被解释为TRUE。如果键出现时没有任何“= Value”部分,则认为它为设置的默认值。如果值为NO, FALSE或O,它将被认为是FALSE,否则,如果键出现在列表中,它将被认为是TRUE。如果键根本不出现,将返回指定的默认值。

CPLStringList oList;
oList.AddString("key1=value1");   //有value
oList.AddString("key2=true");    //value为true
oList.AddString("key3");        //无value
oList.AddString("key4=false";    //value为false
oList.AddString("key5=0");        //value为0
oList.AddString("key6=NO");        //value为NO

bDefault都设置为true

bool bValue1 = oList.FetchBool("key1",true); // true
bool bValue2 = oList.FetchBool("key2",true); // true
bool bValue3 = oList.FetchBool("key3",true); // true
bool bValue4 = oList.FetchBool("key4",true); // false
bool bValue5 = oList.FetchBool("key5",true); // false
bool bValue6 = oList.FetchBool("key6",true); // false

 

FetchBoolean()

FetchBoolean(const char * pszKey,int bDefault )const
实际上,FetchBoolean() 方法只是 FetchBool() 的一个别名,它们的实现代码是完全相同的。因此,可以使用这两个方法中的任何一个来获取布尔类型的值,两者的区别只是方法名的不同

FetchNameValue()

FetchNameValue ( const char * pszName ) const

获取与此键名称关联的值

参数

psz名称 要搜索的键名称

返回:相应的值或 NULL(如果未找到)。返回的字符串不应被修改,并指向可能在将来调用时更改的内部对象状态

CPLStringList oList;
    oList.AddString("key1=value1");
    oList.AddString("key2=value2");

    const char* pszValue1 = oList.FetchNameValue("key1";
    printf("Value of key1: %s\n", pszValue1); 

    const char* pszValue3 = oList.FetchNameValue("key3");
    printf("Value of key3: %s\n", pszValue3); 

Value of key1: value1
Value of key3: (null)

FetchNameValueDef() 

FetchNameValueDef ( const char * pszName,const char * pszDefault ) const

获取与此键名称关联的值

参数:

psz名称 要搜索的键名称。

psz默认 找不到命名条目时返回的默认值。

返回:相应的值或传递的默认值(如果未找到)

CPLStringList oList;
    oList.AddString("key1=value1");
    oList.AddString("key2=value2");

    const char* pszValue1 = oList.FetchNameValue("key1", "default1");
    printf("Value of key1: %s\n", pszValue1); 

    const char* pszValue3 = oList.FetchNameValue("key3", "default3");
    printf("Value of key3: %s\n", pszValue3); 

Value of key1: value1
Value of key3: default3

 

FindName()

FindName(const char * pszKey)const

获取给定名称/值关键字的索引

CPLStringList oList;
    oList.AddString("apple=red");
    oList.AddString("banana=yellow");
    oList.AddString("grape=purple");

    const char* pszName = "banana";
    int nIdx = oList.FindName(pszName);

 CPLString sValue = oList[nIdx];
        cout << "The value of " << pszName << " is " << sValue << endl;
    }

The value of banana is banana=yellow

InsertString()

InsertString ( int nInsertAtLineNo,const char * pszNewLine )

插入到标识位置的列表中

参数

nInsertAtLineNo 要插入的行,在前面插入的行为零。

psz新线 到要插入的行。将复制此字符串

 CPLStringList oList;
    oList.AddString("value1");
    oList.AddString("value2");
    oList.AddString("value3");

    // 在第二个位置插入字符串
    oList.InsertString(1, "value4");

    // 输出字符串列表
    for (int i = 0; i < oList.Count(); i++) {
        printf("%s\n", oList.Item(i));
    }

value1
value4
value2
value3

InsertStringDirect()

InsertStringDirectly ( int nInsertAtLineNo,char * pszNewLine)

插入到标识位置的列表中

参数

nInsertAtLineNo 要插入的行,在前面插入的行为零。  

pszNewLine 对于要插入的行,此字符串的所有权将由对象接管。它必须已在堆上分配

与InsertString() 函数不同,该函数不进行字符串的拷贝,而是直接将指针添加到列表中。这意味着,在添加后,不应该尝试修改原始字符串或释放内存

 CPLStringList oList2;
    oList2.AddString("value1");
    oList2.AddString("value2");
    oList2.AddString("value3");

    // 在第二个位置插入字符串
    oList2.InsertString(1, "value4");

	char pszValue1[] = "value5";
    oList2.InsertStringDirectly(1,pszValue1);

    char pszValue2[] = "value6";
    oList2.InsertStringDirectly(1,pszValue2);

    // 输出字符串列表
    for (int i = 0; i < oList2.Count(); i++) {
        printf("%s\n", oList2[i]);
    }

value1
value6
value5
value4
value2
value3

SetNameValue()

SetNameValue ( const char * pszKey,const char * pszValue )

在列表中设置name=value条目

CPLStringList oList;
    oList.SetNameValue("key1", "value1");
    oList.SetNameValue("key2", "value2");

    // 修改key1的值
    oList.SetNameValue("key1", "newvalue1");

    // 添加新的key-value
    oList.SetNameValue("key3", "value3");

    // 输出列表
    for (int i = 0; i < oList.Count(); i++)
    {
        
        printf("%s=%s\n", oList[i]);
    }
key1=newvalue1
key2=value2
key3=value3

函数默认将新的键值对添加到列表的末尾。如果要插入到指定位置,可以使用InsertNameValue()函数

Sort()

对列表中的条目进行排序,并将列表标记为已排序

CPLStringList oList;
    oList.AddString("apple");
    oList.AddString("orange");
    oList.AddString("banana");
    oList.AddString("grape");
    oList.AddString("pear");

    for (int i = 0; i < oList.size(); i++) 
        cout << oList[i] << endl;
    
    oList.sort();

    for (int i = 0; i < oList.size(); i++) 
        cout << oList[i] << endl;
    

apple
orange
banana
grape
pear
apple
banana
grape
orange
pear

对列表中的条目进行排序,并将列表标记为已排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

场主不吃鍋巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值