编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。


 #include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <cstdlib>

using namespace std;
int main() {
    int a=2 + 3.14, b=3 + 3.3;
    cout << "a, b =" << (true ? 1, 2 : (3, 4) ) << endl;
    int x[10];
    int *p = x;
    cout << "sizeof(x)/sizeof(*x): " << sizeof(x)/sizeof(*x) << endl;
    cout << "sizeof(p)/sizeof(*p): " << sizeof(p)/sizeof(*p) << endl;
    cout << "sizeof(p): " << sizeof(p) << ", sizeof(*p): " << sizeof(*p) <<  ", sizeof(int): " << sizeof(int)<< endl;

    string str="hello world", str2;
    vector<string> book = {"good", "morning", "hello"};
    vector<string> book2;
    cout << "sizeof str: " << sizeof(str) << "," << sizeof str2 
        << " sizeof vector: " << sizeof(book) << "," <<sizeof(vector<int>) << endl;
    for(auto vstr : book){
        cout << vstr << " ";
    }
    cout << endl;

    //auto it = book.begin(); // segmentation fault bcs the iterator point to the old memory but vector memory changed after puchback.
    cout << "befor push: capacity is " << book.capacity() << " size is " << book.size() << endl; 
    char chTemp[1024] = {0};

    for (int i=0; i != 5; i++){
        sprintf(chTemp, "app_%d", i);// error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string}’ to ‘char*’ for argument ‘1’ to ‘int sprintf(char*, const char*, ...)’
        book.push_back(chTemp);
        cout << "capacity is " << book.capacity() << " size is " << book.size() << endl; 
    }

    for (auto it = book.begin(); it != book.end(); it++){
        cout << *it << " ";
    }
    cout << endl;

    int popNum = book.size() -1;
    for (int i=0; i != popNum; i++){ // i != book.size() segmentatin fault
        book.pop_back();
        cout << "after pop" << i << ": capacity is " << book.capacity() << " size is " << book.size() << endl; 
    }

    
    for (auto it = book.begin(); it != book.end(); it++){
        cout << *it << " ";
    }
    cout << endl;

	cout << str << endl;
	return 0;
}