现在的位置: 首页 > 综合 > 正文

C++问题集锦

2014年01月23日 ⁄ 综合 ⁄ 共 4406字 ⁄ 字号 评论关闭

慢慢积累吧。。

问题1

C++二维数组如何做参数

参考自stackoverflow,假设函数类型为void

template<std::size_t N, std::size_t M>
void func(int (&arr)[N][M])
{
   //函数具体内容
}
int arr[2][3]= { 1,2,3,4,5,6 };
 func(arr);


问题2:如何读取二进制文件
首先创建ifstream的一个对象,然后使用该对象的read方法
ios::binary 表示open in binary mode
#include<fstream>
#include<iostream>
using namespace std;
int main(){
	int arr[4];
	ifstream file("D://chauvinist.dat",ios::in | ios::binary);
	file.read(reinterpret_cast<char*>(&arr),sizeof(arr));
	for(int i=0;i<4;i++)
		cout<<arr[i]<<endl;
	system("pause");

}

若要在指定字节处读取,可以使用ifstream中的seekg函数,seekg函数的单个参数版本指定距离文件初始位置的字节数

c++如何动态分配二维数组
假设申请的是m行n列的二维数组
int m;
	int n;
	int ** arr=new *int[m];
	for(int i=0;i<m;i++)
		arr[i]=new int[n];

问题:如何判定一个数的奇偶性

之前一直在用取模运算判定的方式,其实也可以用按位与运算这样更有效率
x与1按位与时,若x低位为1,则结果为1,低位为1时表明该数为奇数
否则则为偶数
if(x&1==1)//为奇数
x&1==0为偶数

通过异或运算交换实现两数的交换

方法如下

a=a^b;
b=b^a;
a=a^b;

设m位上的数为am,bm,记cm=am^bm

若am与bm相同

则am^bm为0,即cm=0, 若bm=0,bm^cm=0=am,若bm=1,bm^cm=1=am

若am与bm不同

则am^bm为1,即cm=1,若bm=1,则am=0,bm^cm=0=am,若bm=0,则am=1,bm^cm=1=am

所以b^(a^b)就得到了a

(a^b)^a就得到了b

这里a与b每个位置上有(2*2)4种可能的排列,列举一下四种排列即可证明

如何将char类型转换成int

可以用如下形式,先用string构造函数构造一个string对象,再通过c_str方法转换成const char*类型

接着便可以调用atoi函数了

char ch

int i= atoi(std::string(1, ch).c_str());

另一种方法为

用该字符减去'0'.

加‘0’与减'0'可以实现字符和该字符表示的数字之间的相互转换

char c='4';
int i=int(c-'0');

C++可变参数数量函数

C提供了eclipse的机制,之前一直没接触过

具体可参考此文http://www.learncpp.com/cpp-tutorial/714-ellipses-and-why-to-avoid-them/

这个机制不建议使用

#include <cstdarg> // needed to use ellipses
// The ellipses must be the last parameter
double FindAverage(int nCount, ...)
{
    long lSum = 0;
 
    // We access the ellipses through a va_list, so let's declare one
    va_list list;
 
    // We initialize the va_list using va_start.  The first parameter is
    // the list to initialize.  The second parameter is the last non-ellipse
    // parameter.
    va_start(list, nCount);
 
    // Loop nCount times
    for (int nArg=0; nArg < nCount; nArg++)
         // We use va_arg to get parameters out of our ellipses
         // The first parameter is the va_list we're using
         // The second parameter is the type of the parameter
         lSum += va_arg(list, int);
 
    // Cleanup the va_list when we're done.
    va_end(list);
 
    return static_cast<double>(lSum) / nCount;
}
 
int main()
{
    cout << FindAverage(5, 1, 2, 3, 4, 5) << endl;
    cout << FindAverage(6, 1, 2, 3, 4, 5, 6) << endl;
}

C++ STL中的set

set中的元素均是唯一的。且set中的元素是有序的。

set的begin方法返回的迭代器

Because set containers
keep their elements ordered at all times, 
begin points to the element that goes first following the container's sorting
criterion
.

注意到C语言字符串最后一个字符为填充字符0,也可以写作'\0'。代表结束字符串的结束符

问题:C++中int类型转换成std中的string类型

string itos(int i) // convert int to string
{
    stringstream s;
    s << i;
    return s.str();
}

在c++11标准中,引入了更简洁的std::tostring方法

为什么需要指针的指针或者指针的引用

可以参考

http://www.codeguru.com/cpp/cpp/cpp_mfc/pointers/article.php/c4089/PointertoPointer-and-ReferencetoPointer.htm

http://stackoverflow.com/questions/4426474/is-passing-pointer-argument-pass-by-value-in-c


函数参数中的指针传递本质上也是值传递,在函数体中修改参数指针的地址不会反映到函数外部

比如

void test1(node *root)

下例中test1对参数root地址的修改根本不会改变调用的参数p,test1(p)

函数体改变的只是p的拷贝。因为这里使用的是值传递。若要通过函数参数去改变指针的地址,可以通过指针的指针(**),或者指针的引用(*&),指针的引用只是C++支持,C语言不支持,若是指针的指针,比如 int i=5;int *p=&i;int q=&p,这里

q便是指针的指针,通过解引用符*,*q即为 p,可以通过*q修改指针p的值。

当指针的指针作为函数参数的时候,比如void  test(node **p) 。*p相当于解一级引用。此时可以通过*p修改函数参数的值。

或者使用指针的引用。这时使用的是引用传递。

#include<iostream>
#include<string>

using namespace std;
struct node{
	char value;
	node *left;
	node *right;
};
node* test(){
	node *root=new node();
	cout<<"inside test"<<root<<endl;
	root->value='D';
	root->left=root->right=NULL;
	return root;
}

void test1(node *root){
	root=new node();
	cout<<"inside test1 "<<root<<endl;
	root->value='1';
	root->left=root->right=NULL;
}
void test2(node **root){
	*root=new node();
	cout<<"inside test2 "<<*root<<endl;
	(*root)->value='2';
	(*root)->left=(*root)->right=NULL;
}
void test3(node *&root){
	root=new node();
	cout<<"inside test3 "<<root<<endl;
	root->value='3';
	root->left=root->right=NULL;
}
int main(){
	node *p=new node();
	cout<<"before test"<<p<<endl;
	p=test();
	cout<<"outside func"<<p<<endl;
	cout<<p->value<<endl;
	test1(p);
	cout<<"outside func"<<p<<endl;
	cout<<p->value<<endl;
	test2(&p);
	cout<<p<<" "<<p->value<<endl;
	test3(p);
	cout<<p<<" "<<p->value<<endl;

}

C语言如何在函数中返回字符串的子串

注意第一种方法是在函数内申请的动态内存空间,需在函数外释放这个空间。

#include<stdio.h>
#include<stdlib.h>
char *sub_str(char* str,int start,int end){
	//return  substring from pos start to end(included)
	char *a=str;
	char *s=(char*)malloc((end-start+1)*sizeof(char));
	char *temp=s;
	//move to the substring start position;
	a=str+start;
	for(int i=start;i<=end;i++){
		*temp++=*a++;
	}
	return s;
}
void sub_str2(char *str,char *sub,int start,int end){
	str=str+start;
	for(int i=start;i<=end;i++)
		*sub++=*str++;
}
int main(){
	char *str="I love apples";
	//c is allocated inside function
	char *c=sub_str(str,2,5);
	
	char *sub=(char*)malloc(4*sizeof(char));
	sub_str2(str,sub,2,5);
	printf("%s\n",c);
	printf("%s\n",sub);
	free(c);
	free(sub);
}

抱歉!评论已关闭.