我的代码中存在一个嵌套字符串向量的问题.它不打印字符串.
void foo(vector<vector<char const *> > const & vcp){
vector<vector<char const *> >::const_iterator i(vcp.begin());
vector<vector<char const *> >::const_iterator e(vcp.end());
for(; i != e; ++i){
vector<char const *>::const_iterator ci(i->begin());
vector<char const *>::const_iterator ce(i->end());
for(; ci != ce; ++ci)
cout<<*ci<<endl; //Not printing
}
}
int main(){
std::vector<vector<char const *> > vvcp(3);
std::vector<char const *> vcp(3);
vcp.push_back(string("abcd").c_str());
vcp.push_back(string("efgh").c_str());
vcp.push_back(string("ijkl").c_str());
vvcp.push_back(vcp);
vvcp.push_back(vcp);
foo(vvcp);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) const 成员函数只调用 const 成员函数吗?
class Transmitter{
const static string msg;
mutable int size;
public:
void xmit() const{
size = compute();
cout<<msg;
}
private:
int compute() const{return 5;}
};
string const Transmitter::msg = "beep";
int main(){
Transmitter t;
t.xmit();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
如果我不使 compute() 成为一个常量,那么编译器就会抱怨。是不是因为 const 成员函数不允许修改成员,它不允许对非常量的任何调用,因为这意味着 const 成员函数将“间接”修改数据成员?
我写了这个小函数来检索列表的尾部:
let getTail l = if length l > 0 then tail l else "empty list"
Run Code Online (Sandbox Code Playgroud)
传递[]给getTail返回empty list但传递[1,2,3]给出以下错误:
<interactive>:1:14:
No instance for (Num Char)
arising from the literal `3'
Possible fix: add an instance declaration for (Num Char)
In the expression: 3
In the first argument of `getTail', namely `[1, 2, 3]'
In the expression: getTail [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我无法理解那个错误意味着什么.问题是什么?使用GHCi 7.0.4
我正在尝试实现factorial的尾递归版本:
let{factorial 0 n = n; factorial x n = factorial (x-1, n * x)}
Run Code Online (Sandbox Code Playgroud)
我明白了:
<interactive>:1:41:
Occurs check: cannot construct the infinite type: t1 = t1 -> t1
In the return type of a call of `factorial'
In the expression: factorial (x - 1, n * x)
In an equation for `factorial':
factorial x n = factorial (x - 1, n * x)
<interactive>:1:52:
Occurs check: cannot construct the infinite type: t0 = (t0, t1)
In the first argument …Run Code Online (Sandbox Code Playgroud) 我想像数组一样构造一个std :: string对象:
std::string str("");
str[0] = 'A';
str[1] = 'b';
str[2] = 'h';
str[3] = 'i';
str[4] = '\0';
std::cout<<str;
Run Code Online (Sandbox Code Playgroud)
但它不打印字符串.我错过了什么?
我有以下代码:
int main(){
const int a = 1;
const int* b(&a);
int* c = const_cast<int*>(b);
*c = 29;
cout<<*c<<a<<*b;
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
为什么'a'的值不变为29?这是否意味着当const_casting b时,a的常量不会被删除?
我试图将一对矢量复制到另一个:
vector<pair<int,int>> vp = {pair<int,int>(1,1), pair<int,int>(2,2)};
vector<pair<int,int>> vp2;
for_each(vp.begin(), vp.end(), [vp2](pair<int,int> p){
if(/*some condition*/){
vp2.push_back(p);
}
});
Run Code Online (Sandbox Code Playgroud)
我得到这个编译器错误:
error: passing ‘const std::vector<std::pair<int, int> >’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >, value_type = std::pair<int, int>]’ discards qualifiers
Run Code Online (Sandbox Code Playgroud)
在ubuntu上使用gcc 4.5.1.
我正在向另一个使用该指针推送数据的函数传递一个向量指针:
void foo(vector<pair<int,int>> * vp){
vp->push_back(pair<int,int>(1,1)); //causes segfault
}
void bar(vector<pair<int,int>> *vp = NULL){
foo(vp);
}
Run Code Online (Sandbox Code Playgroud)
push_back会导致段错误.
在.NET 3.5应用程序中,我想在RichTextBox控件中获取插入位置.RTB不是XAML.此外,RTB没有CaretPosition如下所述的属性:http:
//msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition.aspx
什么是我能得到的最简单的方法插入位置?
编辑:更具体地说,我想从它所在的线的起点找出插入符号的位置.我可以通过使用GetLineFromCharIndex(rtb.SelectionStart)而不是从行首开始的偏移来获取行号.
我如何告诉Python我想要执行以下操作:
if cond1 and (not cond2 or not cond2)
Run Code Online (Sandbox Code Playgroud)
我希望括号中的表达式先执行,然后将结果提供给和.
c++ ×6
vector ×3
haskell ×2
string ×2
c++11 ×1
caret ×1
const-cast ×1
const-method ×1
cstring ×1
expression ×1
factorial ×1
pointers ×1
python ×1
recursion ×1
richtextbox ×1
tail ×1
vb.net ×1