小编xyz*_*xyz的帖子

C++ const 引用允许从表达式进行更改吗?

在《C++ Primer 5th Edition》中有关 const 引用的部分中,有一个小示例块:

int i = 42;
const int &r1 = i; // we can bind a const int& to a plain int object
const int &r2 = 42; // ok: r1 is a reference to const
const int &r3 = r1 * 2; // ok: r3 is a reference to const
int &r4 = r * 2; // error: r4 is a plain, non const reference
Run Code Online (Sandbox Code Playgroud)

在第四行,我只是好奇常量引用的常量引用如何能够成功地将值乘以 2。当 r1 引用 i 时,不会发生转换,使所有内容都成为常量吗?或者第 4 行中的表达式对于 r3 来说是独立的吗?

c++

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

Python字符串切片中的+含义是什么?

我刚开始学习Python并且一直关注Google Python类.在其中一个字符串练习中,有以下代码:

def not_bad(s):
  n = s.find('not')
  b = s.find('bad')
  if n != -1 and b != -1 and b > n:
    s = s[:n] + 'good' + s[b+3:]
  return s
Run Code Online (Sandbox Code Playgroud)

我想知道s [b + 3:]代表什么,因为这是我第一次遇到字符串切片中的+.

python

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

为什么这个C++程序的输出会在cmd中产生巨大的混乱?

我已经弄清楚如何使这个程序正确,但我只是想知道为什么这个错误的程序后面的问题产生了一大堆字符和不同的机器信息,如果这样的错误可能会损坏机器:

练习3.10:编写一个程序,读取包括标点符号在内的字符串,并写入已读取但删除了标点符号的内容.

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;


int main()
{
    string s("Some! string!s.");

     for (decltype(s.size()) index = 0;
        index != s.size(); ++index){
            if (!ispunct(s[index])){
                cout << s[index];
                ++index;
            }
            else {
                ++index;
            }
        }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,我知道这是不正确的,并且已经使这个版本正确输出所需的内容:

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;


int main()
{
    string s("Some! string!s.");

     for (decltype(s.size()) index = 0;
        index != s.size(); ++index){
            if (!ispunct(s[index])){
                cout << s[index];
            }
        }

    return …
Run Code Online (Sandbox Code Playgroud)

c++ string

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

"回归"声明的描述是什么?

我正在通过艰难的方式学习Python,并在练习21中使用return语句.在浏览代码时,我理解return语句正在做什么,但我完全没有得到Zed Shaw的描述.我想确保我没有错过任何东西.练习有这个代码

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = …
Run Code Online (Sandbox Code Playgroud)

python return

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

标签 统计

c++ ×2

python ×2

return ×1

string ×1