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; }