我正在尝试将2D数组传递给函数.我尝试在互联网上提供不同的解决方案.
int arr[3][4];
fun (arr);
1) void fun(int *a[4]) {} -- result into a compilation error (cannot convert int (*)[4] to int **)
2) void fun(int(*a)[4]) {} -- works fine.
Run Code Online (Sandbox Code Playgroud)
我想知道上述两个声明之间有什么区别,以及1. case中有错误.
这三个开源库非常常用于android.我只知道这些库用于处理字体.我在想这些图书馆之间的区别是什么?它们是否相互关联?或者他们可以互相替代.
我写下面的代码我的机器(devcpp)和codepad.org但我的匹配工作正常,在codepad.org(http://codepad.org/XfW5a8en)输出是一个垃圾字符.
Run Code Online (Sandbox Code Playgroud)#include <iostream> #include<cstring> using namespace std; int main () { char *str1 =const_cast<char*>(string("Hello ").c_str()); char *str2 = const_cast<char*>(string("World!").c_str()); char *ptr = str1; char *&rptr = str1; rptr = str2; std::cout << ptr << str1 << std::endl; }
我期待输出为Hello World!
我正在阅读TreeSet并发现它很有趣.我有一个问题让我们有一个TreeSet的String.在这种情况下,这个Ceiling和floor函数将如何表现.
NavigableSet<String> ns= new TreeSet<String>();
ns.add("Yogi");
ns.add("Yogendra");
ns.add("Yogesh");
ns.add("hello");
String ns1= ns.ceiling("Yog");
System.out.println(ns1);
Output==> Yogendra
Run Code Online (Sandbox Code Playgroud) 我写了一个小问题来检查const数据成员上const_cast的行为.
using namespace std;
class myString{
public:
myString(char * str)
{
p=str;
}
const char * getString(){
return p;
}
private:
const char *p;
} ;
int main()
{
char *p=(char*)malloc(8);
cin>>p;
myString *m= new myString(p);
char *s =const_cast<char*>(m->getString());
s[6]='y';
cout<<s<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行此程序后,我将其作为"yogendra"(8个字母的字符串).我把输出称为"yogendya"现在我怀疑了.通过const_cast <>我们可以覆盖数据成员本身的行为,因为这里的字符串是const char*仍然在转换后我可以修改它.
我正在尝试编写一个反转字符串的递归方法,如下所示.
void reverse(string s,int i,int l)
{
static int j;
while(i<l)
{
char ch=s[i];
cout<<ch<<endl;
reverse(s,i+1,l);
cout<<"after="<<ch<<endl;
s[j]=ch;
j++;
}
cout<<s<<endl;
s[j]=0;
}
Run Code Online (Sandbox Code Playgroud)
但我的输出不正确."after="<<ch始终打印字符串的最后一个字符.函数的参数是s是std :: string,i是从0开始的索引,l是字符串的长度.任何人都可以指出我做错事的地方.
有如下四种形状的括号.
类型1:'('或')'
类型2:'{'或'}'
类型3:'['或']'
类型4:'<'或'>'
如果只有如上所示的由四个形状的括号组成的字符串,请编写一个函数以返回最内部括号的深度.深度由重叠程度定义.最外侧托架的深度为1,最外侧托架内侧的托架深度为2,托架内侧的深度为3.
示例: - "{([])[()(<>)]}"此处最大深度为4.让字符串包含有效括号.
我试图在C++中运行C样式代码,我的编译器给出以下错误:
第5行:错误:字符串常量之前的预期unqualified-id
第二:我的目标是理解"第d行"的错误.
using namespace std;
typedef int (*pfun)(int); // line a
int main()
{
extern "C" void foo(pfun); // line b
extern "C" int g(int); // line c
foo( g ); // line d, Error
return 0;
}
Run Code Online (Sandbox Code Playgroud)