String - 一些测试(持续更新)

void main()
{
    char *buffer = new char(1000);
    memset(buffer, 0, 1000);
    char buffer1[1000] = {};
    buffer1[0] = 'a';
    buffer1[1] = 'b';
    buffer1[2] = 'c';
    strcpy(buffer, &buffer1[0]);

/*-------------------------------------------------*/
    string str(buffer);
    string sub = str.substr(0, 3);
    cout << sub.c_str();  //打印abc 
    cout << sub; //打印abc
    cout << sub.back(); //打印c
/*pop_back是删除的意思*/
/*-------------------------------------------------*/
    vector<string> str1;    
    str1.push_back(&buffer[0]);  
    str1.push_back(&buffer[1]);
    str1.push_back(&buffer[2]);
    cout << str1.back(); //打印c
    str1.pop_back();
 //   cout << str1.data;
    cout << str1.back(); //打印bc
    str1.pop_back();
    cout << str1.back(); //打印abc
    str1.push_back(buffer1);
    cout << str1.back(); //打印abc 应该是按照地址打印的
    
/*------------------------------------------------*/
    string str2;
    str2.push_back('a');
    cout << str2; //打印a
    str2.pop_back();
    cout << str2; //没有输出
/*-------------------------------------------------*/
    char buffer3[] = "abcdefg";
    vector<string> str3;
    str3.push_back(buffer3);
    cout << str3.back(); //打印abcdefg
    
    return 0;
}

 


 

#include <string>
#include <vector>
#include <iostream>

int main()
{
    wchar_t test[][8] = { L"first", L"second", L"third", L"fourth" };
    std::vector<std::wstring> vectorOfStrings;
    wchar_t* bufferForStrings;
    size_t i, length = 4;
    for (i = 0; i < length; i++) {
        // copy some string to the buffer...
        bufferForStrings = test[i];
        vectorOfStrings.push_back(bufferForStrings);
    }

    for (auto s : vectorOfStrings) {
        std::wcout << s << std::endl;
    }

    return 0;
}
std::string ExtractId(std::string buffer) {
  int pos1 = std::string(buffer).find("<Id>") + strlen("<Id>");
  int pos2 = std::string(buffer).find("</Id>");

  std::string id = std::string(buffer).substr(pos1, pos2 - pos1);

  return id;
}

 

posted @ 2020-12-09 14:29  strive-sun  阅读(101)  评论(0编辑  收藏  举报