- 博客(18)
- 收藏
- 关注
原创 atoi 把字符串转换成相应的整形数
#include#includeint main() { char s[ 50 ] ; scanf("%s",&s); printf("%d\n",atoi(s)*10); return 0 ;}#include#includeint main() { char s[ 50 ] ; scanf("%s",&s); int a ; sscanf(
2013-12-28 01:25:29 1432
原创 移动数组中的元素
#includeusing namespace std;void leftt(int a[ ],int m){ int temp = a[ 0 ]; for( int i = 1 ; i < m ; i++ ) a[ i - 1 ] = a[ i ]; a[ m - 1 ] = temp; for( i = 0 ; i < m ; i++ ) cout<<a[ i ]<<
2013-12-22 13:49:18 1212
原创 C++ 中*max_element函数找最大元素 *min_element函数找最小元素 STL算法
#include#includeusing namespace std;int main(){ int a[] = {1,2,3,4,5,6,7}; cout<<*max_element(a,a+7)<<endl; cout<<*min_element(a,a+7)<<endl; cout<<a[3]<<endl; return 0;}这种方法比较好的是:不改变原
2013-12-20 10:46:20 1768
原创 带空格的字符串输入问题
#include#includeusing namespace std;int main(){ string s1,s2; char s3[100]; getline(cin,s1); //输入带空格的字符串 cin.getline(s3,100); // 输入带空格的字符 cin>>s2; //输入不带空格的字符串 cout<<s1<<endl<<s2<<e
2013-12-20 09:38:39 1817
原创 最大公约数和最小公倍数 递归算法
#includeusing namespace std;int lcd(int a,int b){ if(b == 0) return a; else return lcd(b,a%b);}int lcm(int a,int b){ int c = lcd(a,b); return a*b/c;}int main(){ int a,b; cin>>a>>b;
2013-12-20 09:29:48 1323
原创 C++ 中的 set
set 和 multiset 区别在于 multiset 中元素允许有重复,而set中元素不允许有重复;#include#includeusing namespace std;int main(){ multiset m; m.insert(1); m.insert(7); m.insert(2); m.insert(5); m.insert(5); for(mult
2013-12-15 11:35:05 1025
原创 sscanf(字符串转化为整数)和sprintf(整数转化为字符串)
#include using namespace std;int main(){ char result[100]="123456789abcdefg", str[100]; int num; sscanf( result,"%d%s", &num,&str ); cout<<num<<endl; cout<<str<<endl; retur
2013-12-13 10:30:15 2733
原创 C++ 中rand() 产生随机数的用法
#include #include using namespace std;int main(){ srand(time(NULL)); char n; n = rand()%26 + 'a'; cout<<n<<endl; return 0;} srand(time(NULL)); 可以使用循环使随机数产生的可能性相同
2013-12-13 10:09:26 1261
原创 C++ 中mallon动态分配内存大小用法
#includeusing namespace std;int main(){ char *s; int n; cin>>n; s= (char *) malloc(n+1); cin>>s; cout<<s<<endl;; free(s); return 0;} ( 数据类型 *) mallon ( 内存大小+1);
2013-12-13 09:48:47 1137
原创 C++ 声明一个变量 和 New 一个空间的区别
C++ 中声明一个变量,这个变量具有一定的作用域和生命周期。作用域: 就是一个变量被引用的范围。生命周期:这个变量可以被引用的时间段,不同生命周期的变量,在程序内存中的分布位置不同,一个程序内存分为:代码区、全局数据区、堆区、栈区,不同的内存区域对应不同的生命周期;如果没有当前函数以外的指针指向它,则在当前函数结束时,会将当前变量析构;如果有函数以外的指针指向它,则不会被析构;
2013-12-10 12:13:31 4274
原创 C++中 map::at 使用方法
map::at 查找具有指定键值的元素 #include #include using namespace std;int main(){ map M; M.insert(map::value_type("Scottding",1)); M.insert(map::value_type("DHL",2)); M.at("Scottdin")++; for(map::
2013-12-08 17:25:33 14689
原创 C++ ceil 和 floor
ceil // 向上取整floor //向下取整#include #include #include using namespace std;int main(){ double a; cin>>a; cout<<ceil(a)<<endl; cout<<floor(a)<<endl; return 0
2013-12-07 15:54:31 1786
原创 string 中的 find()、 find_first_of()、find_first_not_of()
find() //可以在指定字符串中查找完全匹配子串的位置;find_first_of() //查找在字符串中第一个与子串中的某个字符匹配的字符,返回它的位置,如果没找到就返回string::nposfind_first_not_of() //返回在字符串中首次出现的不匹配子串任何字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos。
2013-12-07 15:50:53 1776
原创 C++ 中string.erase() 的用法
1、string.erase(pos,n) //删除从pos开始的n个字符 string.erase(0,1); 删除第一个字符#include #include using namespace std;int main(){ string::iterator i; string s; cin>>s; s.erase(1,2);
2013-12-07 03:26:56 29446 2
原创 C++ 标准库 map 使用方法
map 是一类关联式容器,它的特点是增加和删除元素对迭代器影响很小,除了那个操作元素,对其他的元素都没有什么影响,对于迭代器来说,可以修改实值,但不能修改key;1、自动建立key—value的对应;key和value可以是任意数据类型;2、根据key值快速查找记录,查找的基本复杂度为log(N),1000个记录最多查10次;3、快速插入key—value值;4、快速删除记录;
2013-12-07 02:52:12 953
原创 C++ 标准库 vector 使用方法
向量(vector)相关使用方法:1、push_back //在数组的最后添加一个数据2、pop_back //删除数组的最后一个数据3、at //得到编号位置的数据4、begin //得到数组的头指针5、end //得到数组最后一个单元+1的指针6
2013-12-06 14:01:27 1118
原创 C++标准库 list使用方法
一、list(链表) list 将元素按顺序存储在链表中,与向量(vector)相比,它允许快速的插入和删除,但是随机访问却比较慢 list 对象函数:1、assign() //给list赋值2、back() //返回最后一个元素3、erase() //删除一个元素4、clear()
2013-12-06 11:09:52 2166
转载 非线性数据结构——二叉树
在计算机科学中,树形结构是一类重要的非线性数据结构,二叉树是一种重要的树形结构。 二叉树是n个数据的有限集,它或为空集(n=0),或含有唯一的称为根的元素,其余元素分成两个互不相交的子集,每个子集自身也是一颗二叉树,分别称为根的左子树和右子树,集合为空的二叉树简称为空数,二叉树中的元素也称为结点。 二叉树中其左、右子数均为空的结点称之为叶子结点,反之所有非叶子结点称之为分支结点
2013-12-02 12:27:17 1649
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人