Skip to content

Commit ccda3d2

Browse files
committed
update
1 parent 53f741e commit ccda3d2

File tree

5 files changed

+160
-12
lines changed

5 files changed

+160
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Project exclude paths
2-
/cmake-build-debug/
2+
/cmake-build-debug/
3+
.idea/vcs.xml

8_CorrelatedContainer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ int coutfreq()
2525
it != counters.end(); ++it)
2626
{
2727
cout << it->first << "\t" << it->second << endl;
28+
cout << (*it).first << "\t" << it->second << endl;
2829
}
2930
return 0;
3031
}
3132

3233
// 得到每一行的单词,并记录其出现的位置
3334
// 分割函数作为参数,给函数的功能增加了很多可能性
3435
map<string, vector<int> > xref(istream& in,
35-
vector<string> find_words(const string&) = split)
36+
vector<string> find_words(const string&) = split)
3637
{
3738
string line;
3839
int line_num = 0;

8_MakeaSentence.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Grammar read_grammar(istream& in)
2020
string line;
2121
int line_num = 0;
2222

23-
while (getline(in,line) && line_num<15)
23+
while (getline(in,line) && line_num<14)
2424
{
2525
line_num += 1;
2626
vector<string> entry = split(line);
@@ -61,7 +61,10 @@ void gen_aux(const Grammar& g, const string& word, vector<string>& ret)
6161
{
6262
auto it = g.find(word); //找到 sentence 开头的规则
6363
if (it == g.end())
64+
{
65+
cout << "word" << word << endl;
6466
throw logic_error("empty rule");
67+
}
6568

6669
const Rule_collection c = it->second; // 重定向所有规则
6770

@@ -79,9 +82,7 @@ vector<string> gen_sen(const Grammar& g)
7982
return ret;
8083
}
8184

82-
int main()
83-
{
84-
vector<string> sentence = gen_sen(read_grammar(cin));
85+
void outputSentence(vector<string> sentence){
8586
auto it = sentence.begin();
8687
if (!sentence.empty())
8788
{
@@ -94,17 +95,24 @@ int main()
9495
++it;
9596
}
9697
cout << endl;
97-
return 0;
9898
}
9999

100-
101-
100+
int SENmain()
101+
{
102+
Grammar g = read_grammar(cin);
103+
for(int i=0;i!=10;++i)
104+
{
105+
vector<string> sentence = gen_sen(g);
106+
outputSentence(sentence);
107+
}
108+
return 0;
109+
}
102110

103111
//<none> cat
104112
//<none> dog
105113
//<none> table
106114
//<np> <none>
107-
//<np> <adj><np>
115+
//<np> <adj> <none>
108116
//<adj> large
109117
//<adj> brown
110118
//<adj> absurd
@@ -113,5 +121,5 @@ int main()
113121
//<pos> on the stairs
114122
//<pos> under the sky
115123
//<pos> where it wants
116-
//<sentence> the <np><verb><pos>
124+
//<sentence> the <np> <verb> <pos>
117125
// end-of-file

9_Generics.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,142 @@
77
#include <map>
88
#include <iostream>
99
#include <list>
10+
#include <algorithm>
11+
#include "7_Use library/using library.h"
1012

13+
using namespace std;
14+
15+
template <class T>
16+
T median(vector<T> v)
17+
{
18+
typedef typename vector<T>::size_type vec_sz;
19+
vec_sz size = v.size();
20+
if (size == 0)
21+
throw domain_error("median of an empty vector");
22+
23+
sort(v.begin(), v.end());
24+
vec_sz mid = size / 2;
25+
26+
return size %2 ==0 ? (v[mid] + v[mid+1]) / 2 : v[mid];
27+
}
28+
29+
// 输入迭代器:顺序只读
30+
template <class In, class X>
31+
In find_diy(In begin,In end,const X& x)
32+
{
33+
while(begin!=end && *begin!=x)
34+
++begin;
35+
return begin;
36+
}
37+
38+
// 输出迭代器:顺序只写: 单次不跳过,且不重复写入
39+
template <class In, class Out>
40+
Out copy_diy(In begin,In end,Out dest)
41+
{
42+
while(begin!=end)
43+
*dest++ = *begin++;
44+
45+
return dest;
46+
}
47+
48+
// 正向迭代器:顺序读写--不访问前面一个元素
49+
template <class For, class X>
50+
void replace_diy(For begin, For end, const X& x, const X& y)
51+
{
52+
while (begin != end)
53+
{
54+
if (*begin ==x)
55+
*begin = y;
56+
++begin;
57+
}
58+
}
59+
//双向迭代器:正向迭代器+可逆访问
60+
template <class Bi>
61+
void reverse_diy(Bi begin, Bi end)
62+
{
63+
vector<pair<const int, string> > a;
64+
65+
while (begin!=end)
66+
{
67+
--end;
68+
if (begin != end)
69+
swap(*begin++, *end);
70+
}
71+
}
72+
73+
// 随机访问:相减为整数类型,随机访问,整数相加得到新的迭代器(移位)
74+
75+
template <class Ran, class X>
76+
bool binary_search_diy(Ran begin, Ran end, const X& x)
77+
{
78+
while (begin<end)
79+
{
80+
Ran mid = begin +(end-begin)/2;
81+
if (x<*mid)
82+
end = mid;
83+
else if (*mid <x)
84+
begin = mid +1;
85+
else return true;
86+
}
87+
return false;
88+
}
89+
90+
template <class Out>
91+
92+
void split_iter(const string& str, Out os){
93+
typedef string::const_iterator iter;
94+
vector<string> ret;
95+
96+
iter i = str.begin(); // 字符串可以有迭代器,但是字符数组不可以
97+
while (i!= str.end()){
98+
i = find_if(i,str.end(),not_space);
99+
iter j = find_if(i, str.end(),space);
100+
if (i!= str.end())
101+
*os++ = string(i,j);
102+
i = j;
103+
}
104+
105+
}
106+
107+
#include <iterator>
108+
109+
int main()
110+
{
111+
string str;
112+
vector<string> str_vec;
113+
vector<string> str_vec_copy;
114+
while (getline(cin,str))
115+
{
116+
split_iter(str, ostream_iterator<string>(cout, "\n"));
117+
118+
split_iter(str, back_inserter(str_vec));
119+
bool findthe = binary_search_diy(str_vec.begin(),str_vec.end(), "the");
120+
cout << "found the: " << findthe << endl;
121+
122+
auto findthe2 = find_diy(str_vec.begin(),str_vec.end(), "the");
123+
cout << "found the at: " << *findthe2 << endl;
124+
125+
126+
reverse_diy(str_vec.begin(), str_vec.end());
127+
cout << "after reverse: ";
128+
for (auto it = str_vec.begin(); it != str_vec.end(); it++)
129+
cout << (*it) << " " ;
130+
131+
reverse_diy_noswap(str_vec.begin(), str_vec.end());
132+
cout << "after reverse_noswap: ";
133+
for (auto it = str_vec.begin(); it != str_vec.end(); it++)
134+
cout << (*it) << " " ;
135+
136+
replace_diy(str_vec.begin(), str_vec.end(), "t", "r");
137+
cout << endl << "after replace_diy: ";
138+
for (auto it = str_vec.begin(); it != str_vec.end(); it++)
139+
cout << (*it) << " ";
140+
141+
copy_diy(str_vec.begin(), str_vec.end(), back_inserter(str_vec_copy));
142+
cout << endl << "after copy_diy: ";
143+
for (auto it = str_vec_copy.begin(); it != str_vec_copy.end(); it++)
144+
cout << (*it) << " ";
145+
146+
}
147+
}
148+
//I like playing the piano r t

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(Accleratedcpp)
33

44
set(CMAKE_CXX_STANDARD 11)
55

6-
add_executable(Accleratedcpp "5_organize program_data/original/pmain.cpp" "1_Hello World.cpp" 2_chars.cpp "3_loop and count.cpp" "4_use batch data.cpp" "5_organize program_data/original/grade.h" "5_organize program_data/original/Student_info.h" "5_organize program_data/original/Student_info_funcs.cpp" "5_organize program_data/original/median_funcs.cpp" "5_organize program_data/original/median.h" "5_organize program_data/original/grade_funcs.cpp" "7_Use library/use library.cpp" "5_organize program_data/memOptim/main.cpp" "5_organize program_data/memOptim/grade.h" "5_organize program_data/memOptim/grade_funcs.cpp" "5_organize program_data/memOptim/Student_info.h" "5_organize program_data/memOptim/Student_info_funcs.cpp" "5_organize program_data/main.cpp" "6_UseIterator extract_fail.cpp" "7_Use library/find_url.cpp" "7_Use library/StudentGrades.cpp" "7_Use library/6-1.cpp" "7_Use library/using library.h" 8_CorrelatedContainer.cpp "7_Use library/StudentGrade.h" 8_MakeaSentence.cpp)
6+
add_executable(Accleratedcpp "5_organize program_data/original/pmain.cpp" "1_Hello World.cpp" 2_chars.cpp "3_loop and count.cpp" "4_use batch data.cpp" "5_organize program_data/original/grade.h" "5_organize program_data/original/Student_info.h" "5_organize program_data/original/Student_info_funcs.cpp" "5_organize program_data/original/median_funcs.cpp" "5_organize program_data/original/median.h" "5_organize program_data/original/grade_funcs.cpp" "7_Use library/use library.cpp" "5_organize program_data/memOptim/main.cpp" "5_organize program_data/memOptim/grade.h" "5_organize program_data/memOptim/grade_funcs.cpp" "5_organize program_data/memOptim/Student_info.h" "5_organize program_data/memOptim/Student_info_funcs.cpp" "5_organize program_data/main.cpp" "6_UseIterator extract_fail.cpp" "7_Use library/find_url.cpp" "7_Use library/StudentGrades.cpp" "7_Use library/6-1.cpp" "7_Use library/using library.h" 8_CorrelatedContainer.cpp "7_Use library/StudentGrade.h" 8_MakeaSentence.cpp 9_Generics.cpp)

0 commit comments

Comments
 (0)