小编bad*_*ash的帖子

遍历嵌套的字符串向量

我的代码中存在一个嵌套字符串向量的问题.它不打印字符串.

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)

c++ string vector cstring

2
推荐指数
1
解决办法
410
查看次数

const 成员函数只能调用 const 成员函数?

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 成员函数将“间接”修改数据成员?

c++ const-method

2
推荐指数
1
解决办法
2377
查看次数

将非空列表传递给函数

我写了这个小函数来检索列表的尾部:

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

haskell tail parameter-passing

2
推荐指数
1
解决办法
893
查看次数

无法构造无限类型 - 实现尾递归因子计算器

我正在尝试实现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)

recursion haskell tail-recursion factorial

2
推荐指数
1
解决办法
447
查看次数

像数组一样构造std :: string

我想像数组一样构造一个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)

但它不打印字符串.我错过了什么?

c++ string

1
推荐指数
1
解决办法
250
查看次数

const_casting问题

我有以下代码:

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的常量不会被删除?

c++ const-cast

1
推荐指数
1
解决办法
758
查看次数

复制对的矢量

我试图将一对矢量复制到另一个:

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.

c++ vector c++11

1
推荐指数
2
解决办法
1906
查看次数

将向量指针传递给函数会导致段错误

我正在向另一个使用该指针推送数据的函数传递一个向量指针:

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会导致段错误.

c++ pointers vector segmentation-fault

0
推荐指数
1
解决办法
1351
查看次数

获取RichTextBox中的插入符号位置

在.NET 3.5应用程序中,我想在RichTextBox控件中获取插入位置.RTB不是XAML.此外,RTB没有CaretPosition如下所述的属性:http: //msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition.aspx 什么是我能得到的最简单的方法插入位置?

编辑:更具体地说,我想从它所在的线的起点找出插入符号的位置.我可以通过使用GetLineFromCharIndex(rtb.SelectionStart)而不是从行首开始的偏移来获取行号.

vb.net richtextbox caret

0
推荐指数
1
解决办法
6357
查看次数

分组逻辑表达式

我如何告诉Python我想要执行以下操作:

if cond1 and (not cond2 or not cond2)
Run Code Online (Sandbox Code Playgroud)

我希望括号中的表达式先执行,然后将结果提供给和.

python expression

-2
推荐指数
1
解决办法
3496
查看次数