#include "stdafx.h"
#include "iostream"
#include "string"
#include "vector"
using namespace std;
void charArrayOper(){
char str[] {"Iam Ok!"};
printf("%s\n", str);
printf("%d\n", sizeof(str));
printf("%d\n", strlen(str));
char str2[100] = {"abc"};
printf("%d\n", sizeof(str2));
printf("%d\n", strlen(str2));
char * p = "test";
char * q = "test";
cout << "&p=" << p << endl;
cout << "&q=" << q << endl;
printf("&p = %p\n", p);
printf("&q = %p\n", q);
}
void stringObjOperat(){
string s(5, 'n');
cout << s.c_str() << endl;
s.empty();
s.size();
s.length();
s[s.length()-1];
string s1;
string s2 = s + s1;
s1 == s2 ? true : false;
cout << s1 << endl;
for (auto &c : s){
c = toupper(c);
}
}
void testVector(){
struct MyStruct
{
int num;
char sex;
};
vector<MyStruct> v;
vector<int> vi;
vi.push_back(2);
vector<int> vi1 = vi;
vector<int> vi2(vi);
vector<string> vstr = { "avn", "def", "sss" };
vector<int>(5, -22);
vector<string>(100);
vi.empty();
vi.push_back(5);
vi.size();
vi[vi.size() - 1];
vi.clear();
vi != vi1 ? true : false;
for (auto &item : v)
{
item.num *= 2;
}
}
class testVector{
public:
static int * testIterator;
};
void testIterator(){
vector<int>::iterator;
testVector::testIterator;
vector<int> vi = { 100, 200, 300 };
for (vector<int>::iterator iter = vi.begin(); iter != vi.end(); iter++)
{
cout << *iter << endl;
}
for (vector<int>::reverse_iterator ra = vi.rbegin(); ra != vi.rend(); ra++)
{
cout << *ra << endl;
}
}
void testConst_iterator(){
vector<int> vi{ -10, -20, -30 };
int cnt = 0;
for (vector<int>::const_iterator c_iter = vi.begin(); c_iter != vi.end(); c_iter++)
{
cout << *c_iter << endl;
cnt++;
if (cnt == 1)
{
vi.push_back(888);
break;
}
}
for (vector<int>::const_iterator c_iter = vi.begin(); c_iter != vi.end(); c_iter++)
{
cout << *c_iter << endl;
}
}
void insertToVector(){
cout << "连续插入数据带vector" << endl;
vector<int> vi = { 1, 2, 3 };
int cnt = 5;
auto p = vi.begin();
while (p != vi.end())
{
p = vi.insert(p, 6);
if (cnt >= 18)
{
break;
}
++cnt;
p++;
}
auto pp = vi.begin();
while (pp != vi.end())
{
cout << *pp << endl;
++pp;
}
auto ppp = vi.begin();
while (!vi.empty())
{
ppp = vi.erase(ppp);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char str [] { "ssss" };
charArrayOper();
testVector();
testIterator();
testConst_iterator();
insertToVector();
return 0;
}
|