小编qed*_*qed的帖子

基于 C++ 范围的 for 循环中的&符号

这是代码:

#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdio>

int main(int argc, char *argv[]) {
    const unsigned int max_chars = 100;
    char buffer[max_chars];
    std::cin.getline(buffer, max_chars, '\n');
    unsigned int count = 0;
    for (auto c : buffer) {
        if (c == '\0') {
            break;
        }
        count++;
    }
    std::cout << "Input: ===========" << std::endl;
    std::cout << buffer << std::endl;
    std::cout << "Number of chars ==" << std::endl;
    std::cout << std::dec << count << std::endl;
    std::cout << "==================" << std::endl;

} …
Run Code Online (Sandbox Code Playgroud)

c++ reference

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

何时可以在Scala中安全地省略括号?

这是一个玩具示例:

object Example {

  import collection.mutable
  import collection.immutable.TreeSet

  val x = TreeSet(1, 5, 8, 12)
  val y = mutable.Set.empty ++= x
  val z = TreeSet.empty ++ y
  // This gives an error: unspecified parameter
  //  val z = TreeSet.empty() ++ y

}
Run Code Online (Sandbox Code Playgroud)

显然TreeSet.empty,TreeSet.empty()并不是一回事.引擎盖下发生了什么?我什么时候可以安全地省略(或在这种情况下不省略)括号?


更新

我已经将一些代码发送到控制台,然后在eval上面的代码中将其删除,然后再执行以下代码:

  implicit object StringOrdering extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o1.length - o2.length
    }
  }
  object StringOrdering1 extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o2.length …
Run Code Online (Sandbox Code Playgroud)

syntax scala

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

为什么可变和不可变集合中的类型推断表现得如此不同?

运行以下代码时,我得到了一些奇怪的结果:

object Example {

  implicit object StringOrdering extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o1.length - o2.length
    }
  }
  object StringOrdering1 extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o2.length - o1.length
    }
  }


  import collection.mutable
  import collection.immutable.TreeSet

  val x = TreeSet(1, 5, 8, 12)
  val y = mutable.Set.empty ++= x // mutable.Set[Int]
  val y1 = mutable.Set.empty[Int] ++= x // mutable.Set[Int]
  val z = TreeSet.empty ++ y // Set[Any]
  val z1 = TreeSet.empty[Int] ++ y …
Run Code Online (Sandbox Code Playgroud)

scala type-inference

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

在java中查找Map时出现空点异常.

这是代码:

import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IDEA on 13/06/15.
 */
public class ShiftCodes {
    private final Map<byte[], Byte> shiftMap;

    public ShiftCodes(int[][] collapseMatrix) {
        shiftMap = new HashMap<byte[], Byte>();
        for (int i = -128; i < 128; i++) {
            for (int j = -128; i < 128; i++) {
                byte b1 = (byte) i;
                byte b2 = (byte) j;
                byte[] k = new byte[] {b1, b2};
                Byte v = new Byte(GenoBytes.genoByte(GenoBytes.collapse(
                        b1, b2, collapseMatrix)));
                shiftMap.put(k, v); …
Run Code Online (Sandbox Code Playgroud)

java

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

cin没有在这个范围内用g ++声明错误

这是代码:

#include <stdlib.h>
#include <iostream>
int main() {
    std::cout << "Enter two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std:cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << "and " v2 << "is " << v1+v2 << std:endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

g++ x.cpp 
#x.cpp: In function ‘int main()’:
#x.cpp:23:9: error: ‘cin’ was not declared in this scope
#x.cpp:23:9: note: suggested alternative:
#In file included from x.cpp:19:0:
#/usr/include/c++/4.7/iostream:61:18: note:   ‘std::cin’
#x.cpp:24:48: …
Run Code Online (Sandbox Code Playgroud)

c++ g++

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

标签 统计

c++ ×2

scala ×2

g++ ×1

java ×1

reference ×1

syntax ×1

type-inference ×1