Skip to content

Commit

Permalink
add tpl
Browse files Browse the repository at this point in the history
  • Loading branch information
hoterran committed Jan 9, 2014
1 parent d35b286 commit 212042b
Show file tree
Hide file tree
Showing 65 changed files with 1,492 additions and 269 deletions.
57 changes: 46 additions & 11 deletions c++/cprimer/alg.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,67 @@
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
#include <iostream>
#include "p.hpp"

using namespace std;

void printlist(list<int> i) {
list<int>::iterator iter = i.begin();
list<int>::iterator iter_end = i.end();

for (; iter != iter_end; ++iter) {
cout << *iter << "\n";
}
bool compare(int a, int b) {
return !(b > a); //revert
}

int main() {
int ia[5] = {1, 2,3,4,5};
vector <string> svec;
int ia[5] = {1, 2,10,4,5};
vector <int> svec;
list <int> dlist;

vector<string>::iterator viter;
list<int>::iterator liter;

dlist.insert(dlist.begin(), ia, ia + 5);

liter = find(dlist.begin(), dlist.end(), 1);
liter = find(dlist.begin(), dlist.end(), 10);

dlist.insert(liter, 100);
printlist(dlist);
PRINT_ELEMENTS(dlist);
dlist.sort();
PRINT_ELEMENTS(dlist);

svec.insert(svec.begin(), ia, ia+5);
PRINT_ELEMENTS(svec);
sort(svec.begin(), svec.end());
PRINT_ELEMENTS(svec);
sort(svec.begin(), svec.end(), compare);
PRINT_ELEMENTS(svec);

cout << *min_element(svec.begin(), svec.end()) << endl;
cout << *max_element(svec.begin(), svec.end()) << endl;

vector<int>::iterator iter = find(svec.begin(), svec.end(), 10);
PRINT_ELEMENTS(svec, "before ");
reverse(iter, svec.end());
PRINT_ELEMENTS(svec, "reverse ");

//copy

vector<int> d;
copy(svec.begin(), svec.end(), back_inserter(d));
PRINT_ELEMENTS(svec, "copy back");

deque<int> d2;
copy(svec.begin(), svec.end(), front_inserter(d2));
PRINT_ELEMENTS(svec, "copy front");

set<int> d3;
copy(svec.begin(), svec.end(), inserter(d3, d3.begin()));
PRINT_ELEMENTS(svec, "copy inserter");

//find
vector<int>::iterator pos1, pos2;
pos1 = find(d.begin(), d.end(), 2);
pos2 = find(d.begin(), d.end(), 4);

cout << "max: " << *max_element(pos1, pos2) << endl;
}
45 changes: 21 additions & 24 deletions c++/cprimer/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
#include <vector>
#include <iostream>
#include <list>
#include "p.hpp"

using namespace std;

void printlist(list<string> i) {
list<string>::iterator iter = i.begin();
list<string>::iterator iter_end = i.end();

for (; iter != iter_end; ++iter) {
cout << *iter << "\n";
}
}

void print(vector<string> ivec) {
vector<string>::iterator iter = ivec.begin();
vector<string>::iterator iter_end = ivec.end();

for (; iter != iter_end; ++iter) {
cout << *iter << "\n";
}
}

int main()
{
//---- vector
Expand All @@ -41,27 +24,27 @@ int main()
ivec2.push_back("xx");
cout << ivec2.size() << "-" << ivec2.capacity() << endl;
}
print(ivec2);
PRINT_ELEMENTS(ivec2);

// copy
vector<string> ivec3(ivec2.begin(), ivec2.end());
print(ivec3);
PRINT_ELEMENTS(ivec3);

// list
vector <string> svec;
svec.insert(svec.begin(), "abc");
svec.insert(svec.begin(), "def");
print(svec);
PRINT_ELEMENTS(svec);

list <string> slist;
slist.insert(slist.begin(), "cde");
slist.insert(slist.begin(), "oiu");
slist.insert(slist.end(), "mnh");
printlist(slist);
PRINT_ELEMENTS(slist);
slist.pop_back();
slist.pop_front();
cout <<"--" << "\n";
printlist(slist);
PRINT_ELEMENTS(slist);
cout <<"--" << "\n";

vector<string> vec3;
Expand All @@ -73,11 +56,25 @@ int main()
string s[4] = {"abc", "def", "jjh", "oii"};
vec3.insert(vec3.end(), s, s + 4);

print(vec3);
PRINT_ELEMENTS(vec3);

// copy

vector<string> vec4 = vec3; //deep copy
vec3.pop_back();
cout << vec4.size() << endl;

// size_type
typedef vector<string>::size_type vec_sz;
vec_sz m = svec.size();
svec.push_back("a");
cout << "-" << m << "-" << svec.size() << endl;

// copy
vector<string> v2;
v2.resize(svec.size()); // must , avoid segmentfault
copy(svec.begin(), svec.end(), v2.begin());
PRINT_ELEMENTS(v2, "copy "ck_inserter);


}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions clear_non_ascii_or_utf8.c → c/clear_non_ascii_or_utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ bool clear_non_ascii_or_utf8(char* str, int length)
chr <<= 1;
nBytes++;
}
if (nBytes < 2 || nBytes > 4) {
if (nBytes < 2 || nBytes > 6) {
// non 110x
str[i] = '_'; //第一个字节最少为110x xxxx
str[i] = '-'; //第一个字节最少为110x xxxx
i++;
nBytes = 0;
continue;
Expand All @@ -42,11 +42,11 @@ bool clear_non_ascii_or_utf8(char* str, int length)
// non 10xx
for(; utf8Start < i; utf8Start++) {
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '_';
str[utf8Start] = '-';
}
utf8Start = 0;
if ((str[i] & 0x80) != 0)
str[i] = '_';
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '-';
i++;
nBytes = 0;
continue;
Expand All @@ -62,7 +62,7 @@ bool clear_non_ascii_or_utf8(char* str, int length)
// non complete end
for(; utf8Start < i; utf8Start++) {
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '_';
str[utf8Start] = '-';
}
utf8Start = 0;
nBytes = 0;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 6 additions & 7 deletions is_ascii_or_utf8.c → c/is_ascii_or_utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ bool is_ascii_or_utf8(const char* str, int length)

int main(int argc, char* argv[])
{
FILE *f = fopen(argv[1], "r");
char s[100] = {};
fread(s, 1, 100, f);
int l = strlen(s);
int i;

printf("%d\n", is_ascii_or_utf8("cc", strlen("cc")));
printf("%d\n", is_ascii_or_utf8("曹操", strlen("曹操")));

FILE *f = fopen(argv[1], "r");
char s[3000];

while (NULL != fgets(s, (int)sizeof(s), f)) {
printf("%d\n", is_ascii_or_utf8(s, strlen(s)));
}
printf("%d\n", is_ascii_or_utf8(s, strlen(s)));

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 212042b

Please sign in to comment.