C/CPP学习
文章平均质量分 54
qq_17804453
这个作者很懒,什么都没留下…
展开
-
输出表达式中的顺序点问题
#include"iostream"using namespace std;//返回c第一次出现的位置//引用形参负责统计c出现的次数string::size_type foo(const string &s, char c, string::size_type &occurs){ auto ret =s.size();原创 2014-07-17 16:35:33 · 186 阅读 · 0 评论 -
mian函数接受两个实参, 连成s…
#include"iostream"int main(int argc, char **argv) { std::stringstr; for (size_ti = 0; argv[i]; ++i) str += argv[i]; std::cout return0;}原创 2014-07-17 16:36:06 · 216 阅读 · 0 评论 -
返回数组指针的函数
int array[10];int (*func(int i))[10];using arrInt = int[10];arrInt *func2(int i);typedef int arrayInt[10];arrayInt *func3(int i);auto func4 (int i) -> int(*)[10];decltype(array) *func5(int原创 2014-07-17 16:36:08 · 245 阅读 · 0 评论 -
声明vector对象保存函数指针
#include #include using namespace std;//以下任意一种定义方式均合法//using funcp = int(*) (int, int);//typedef int(*funcp) (int, int);//若已知目标类型为Func, 则可以//int Func(int, int);//typedef decltype(Func) *func原创 2014-07-17 16:36:11 · 463 阅读 · 0 评论 -
静态成员在类中的初始化
class circle{ int a; // 普通变量,不能在类中初始化 static int b; // 静态变量,不能在类中初始化 static const int c=2; // 静态常量,可以在类中初始化 static const double PI=3.1416;//error C2864:只有静态常量整型数据成员原创 2014-07-17 16:36:15 · 225 阅读 · 0 评论 -
输入一串字符, 统计特定字符个数
include include include int main(int argc, char **argv){ int a = 0, b = 0, c = 0; string str; getline(cin, str); for(auto it = str.begin(); it != str.end(); ++it){原创 2014-07-17 16:36:18 · 693 阅读 · 0 评论 -
捕获一个int变量, 判断是否…
int main(int argc, char **argv){ int i; cout cin >>i; autoif_equal_zero = [&]()-> bool{ if (i == 0) return true; else while (i > 0)原创 2014-07-17 16:36:22 · 197 阅读 · 0 评论 -
简单的读写文件
int main (int argc, char *argv[]){ string file= argv[1] ; cout ofstream out(file, ofstream::app); stringword; out <<endl; while (cin>> word) out ifstream i原创 2014-07-17 16:36:26 · 200 阅读 · 0 评论 -
使用流迭代器, sort, co…
int main (){ cout //定义输入流迭代器和尾后迭代器 istream_iterator input_number_it (cin),eof; //用迭代器与尾后迭代器初始化vector vectornumbers(input_number_it, eof); sort(numbers.begin (), numbers.e原创 2014-07-17 16:36:33 · 287 阅读 · 0 评论 -
约瑟夫环
题目:约瑟夫环约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。int Josephus (int sum, int signal){ if (sum <1 || signal原创 2014-07-17 16:36:35 · 175 阅读 · 0 评论 -
读入文件, 排除忽略单词后统…
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); ifstreamfile; file.open(argv[1]); mapword_count; setexclude ={ "Hello,", "World!" }; string word,lin原创 2014-07-17 16:36:44 · 192 阅读 · 0 评论 -
读入一个string序列和一个int序列,…
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); vector<string > fuck{ "fuck", "fuck", "fuck", "fuck", "fuck", "fuck","fuck", "fuck", "fuck", "fuck", }; vector<int原创 2014-07-17 16:36:48 · 291 阅读 · 0 评论 -
multiset<string>c与vector&…
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); //将范围从v.begin()到v.end()的元素copy到c的end()处 copy(v.begin (), v.end (), inserter (c, c.end ())); //错误,set不支持back_ins原创 2014-07-17 16:36:55 · 418 阅读 · 0 评论 -
一个异常抛出的例子
#include"iostream"#include"stdexcept"using namespace std;int main(){ int num,num2; while (cin>> num >> num2) { try { if (num2 == 0) throw runtime_err原创 2014-07-17 16:35:35 · 236 阅读 · 0 评论 -
使用迭代器输出vector中的内容
#include "iostream"#include "string"#include "cctype"#include "vector"using namespace std;int main(){ vectorstr; stringt; //结束输入需要Ctrl+Z while (cin>> t) { str.push_ba原创 2014-07-17 16:35:31 · 529 阅读 · 0 评论 -
插入排序与快速排序
#include"iostream"#define ARR_SIZE(a) (sizeof((a)) / sizeof((a[0])))int a[] = {1, 23, 32, 0, 2, 5, 23};void insert_sort(int *a, int n) { int i, j,temp; for (i = 1;i temp =原创 2014-07-17 16:35:38 · 212 阅读 · 0 评论 -
删除容器中重复字符串并按长度排序…
#include >#include >#include >#include >using namespace std;string make_plural(size_t ctr, const string &word, const string&ending){ return (ctr> 1) ? word + ending : word;}bo原创 2014-07-17 16:36:20 · 246 阅读 · 0 评论 -
找出最大的有由个三位数乘积构成的…
一个很重要的想法来自http://zhex.iteye.com/blog/492722提到的:3. 两个三位数的乘积肯定是一个六位数, 而这个六位回文数数P我们可以用xyzzyx的形式表示,那我们可以得到下面的公式:P = 100000x + 10000y + 1000z + 100z + 10y + xP = 100001x + 10010y + 1100zP = 11(9091x +原创 2014-07-17 16:36:24 · 493 阅读 · 0 评论 -
插入排序与快排
#include#define ARR_SIZE(a) (sizeof((a)) / sizeof((a[0])))int a[] = {1, 23, 32, 0, 2, 5, 23};void insert_sort(int *a, int n) { int i, j,temp; for (i = 1;i temp = a[i];原创 2014-07-17 16:36:29 · 256 阅读 · 0 评论 -
接受一个&istream参数, …
此函数从给定流中读取数据, 直至遇到文件结束标识时停止, 他将读取的数据打印在标准输出上, 完成这些之后, 在返回流之前对其进行复位, 使其处于有效状态.istream& ReadWordsFormConsole (istream & input){ stringword; vectorwords; while (input>> word || !input.e原创 2014-07-17 16:36:31 · 348 阅读 · 0 评论 -
数组同时求极值
题目:假设你有一个数组,其中的第 i 个元素代表给定的第 i 天的股票价格。如果你被允许至多完成一个交易(如,买一和卖一股票),设计一个算法找出最大的利润。解决思路:首先赋首元素的值给最小,依次向后计算利润,每次与最大值比较并保存新的最大值和新的最小值。int MaxProfit (vector prices){ int *min =&prices[0]; int原创 2014-07-17 16:36:37 · 510 阅读 · 0 评论 -
用反向迭代器和普通迭代器逆序输出…
int main (int argc, char* argv[]){ vectornumbers; cout for (int i =0; i numbers.push_back (i); cout } cout<< endl << "---------------------------------------"原创 2014-07-17 16:36:39 · 392 阅读 · 0 评论 -
list版本删除重复元素
list& DeleteRepeatElements (list&lst){ lst.sort(); lst.unique(); for (conststring &each_word : lst) cout cout returnlst;}list有自己的一套算法, 应优先考虑, 其中的unique()和sort()原创 2014-07-17 16:36:42 · 207 阅读 · 0 评论 -
读取文件, 统计字符, 忽…
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); ifstreamfile; file.open(argv[1]); mapword_count; string word,line; while(getline (file, line)) {原创 2014-07-17 16:36:46 · 202 阅读 · 0 评论 -
不用加减乘除做加法 代码(C)
引用地址: http://blog.csdn.net/caroline_wendy题目: 写一个函数,求两个整数之和, 要求在函数体内不得使用+, -, *, /四则运算符号.不能使用运算符号,使用位运算,第一步异或运算选位,第二步与运算进位.代码:[cpp] view plaincopy代码(C)" />代码(C)" /> #include #原创 2014-07-17 16:36:53 · 258 阅读 · 0 评论 -
用insert重写单词计数程序
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); //忽略列表 setexclude ={ "fuck", "damn" }; stringword; map<string, size_t> word_count; while (cin>> word)原创 2014-07-17 16:36:57 · 309 阅读 · 0 评论 -
输入孩子的姓名, 用map存储,&…
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); stringfirstName, lastName; multimap<string, vector>kidsNames; while (cin>> firstName >> lastName) { ve原创 2014-07-17 16:36:59 · 272 阅读 · 0 评论 -
文本转换小程序
#include < iostream> #include < fstream> #include < sstream> #include < algorithm> #include < map> #include < string> using namespace std;map< string,string原创 2014-07-17 16:37:02 · 492 阅读 · 0 评论