小编ale*_*per的帖子

为什么Java要我再次按Enter键?

我一直试图理解Scanner的工作方式,一直绞尽脑汁.所以这是代码:

Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String p = sc.nextLine();
System.out.println(s);
System.out.println(p);
System.out.println(sc.hasNextLine());
Run Code Online (Sandbox Code Playgroud)

期待的是:

Love is good  <- press ENTER
Love is blind <- press ENTER
Love is good  <- output
Love is blind <- output
false
Run Code Online (Sandbox Code Playgroud)

什么:

Love is good   <- I press ENTER
Love is blind  <- I press ENTER
Love is good   <- output
Love is blind  <- output
<- press ENTER
true <- output
Run Code Online (Sandbox Code Playgroud)

不明白的是:

  1. 而不是立即打印此行 …

java java.util.scanner

14
推荐指数
5
解决办法
2474
查看次数

Java StringBuilder.setLength() - 是时间复杂度O(1)?

我打算在StringBuilders中执行大量删除最后一个字符的操作.使用的解决方案sb.setLength(sb.length() - 1);对我来说很好看.但是,由于这些删除将处于循环中,我需要知道它的复杂性.

我理解它的方式是这个操作简单地减少了我的StringBuilder对象的一些私有属性,并且不对字符本身执行任何复制/克隆/复制,因此它是O(1)及时并且应该快速工作.

我对吗?

java big-o time-complexity

7
推荐指数
2
解决办法
1009
查看次数

从 stdin 输入 std::string 之前是否必须初始化它?

我需要string在 C++ 中声明一个新变量,然后从标准输入读取它。

我是否必须先初始化字符串,或者这是一个不必要的步骤?换句话说,哪个代码更好:选项 1 或选项 2(或者它们是否相同)?

1)

string s = ""; 
cin >> s;
Run Code Online (Sandbox Code Playgroud)
string s; 
cin >> s;
Run Code Online (Sandbox Code Playgroud)

c++ initialization input

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

为什么我不能在C++中将一个迭代器分配给另一个迭代器?

我正在写这个函数

vector<string> SplitIntoWords(const string& s) {
    vector<string> v_str = {};
    string::iterator str_b;
    str_b = begin(s);

    // TODO some action here

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

我需要声明一个迭代器,它将等于字符串s的开头,这是我函数中的一个参数.

问题出在线上str_b = begin(s);- 代码不能用它编译.为什么这样,我该如何解决?

c++ iterator

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

链表的复制构造函数导致内存错误

我正在编写自己的链表类(用于教育目的),这里是:

我的代码

#include <iostream>

using namespace std;

#define PRINT(x) #x << " = " << x << " "

struct ListNode {
  int val;
  ListNode* next = nullptr;
  ListNode(int x) : val(x), next(nullptr) {}
};

class LinkedList {
private:
  ListNode* _head;
  unsigned long long int _size;
public:

  LinkedList() :_head(nullptr), _size(0) {}

  LinkedList(ListNode* _h) :_head(_h), _size(0) {
    ListNode* node = _head;
    while (node != nullptr) {
      _size++;
      node = node->next;
    }
  }

  // Copy constructor
  LinkedList(const LinkedList& obj) {
    ListNode* node …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list copy-constructor

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

是否可以为#include使用某种路径?

我在文件中有一堆#include语句:

#include "/Users/cooper/Desktop/MyLib/graph_api.h"
#include "/Users/cooper/Desktop/MyLib/mst.h"
#include "/Users/cooper/Desktop/MyLib/dfs.h"
#include "/Users/cooper/Desktop/MyLib/bfs.h"
#include "/Users/cooper/Desktop/MyLib/topo_sort.h"
#include "/Users/cooper/Desktop/MyLib/scc.h"
#include "/Users/cooper/Desktop/MyLib/bipartite.h"
#include "/Users/cooper/Desktop/MyLib/dijkstra.h"
#include "/Users/cooper/Desktop/MyLib/union_find.h"
#include "/Users/cooper/Desktop/MyLib/my_string.h"
#include "/Users/cooper/Desktop/MyLib/2d_array.h"
Run Code Online (Sandbox Code Playgroud)

但是,它可能会在未来发生变化,我将不得不更新许多行.有没有可能有类似的东西

PATH = "/Users/cooper/Desktop/MyLib/
#include PATH + "2d_array.h"
Run Code Online (Sandbox Code Playgroud)

c++

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

这是我的Node结构的正确析构函数吗?

我有这个C++结构:

struct Node {
    char symbol;
    unsigned int index;
    vector<Node*> next;

    // Constructors
    Node():symbol('$'), index(0), next(0) {}
    Node(char &c, const unsigned int &ind):symbol(c), index(ind), next(0) {}

    // Add a new character
    Node* add(char &c, const unsigned int &num) {
        Node *newChar = new Node(c, num);
        next.push_back(newChar);
        return newChar;
    }

    // Destructor
    ~Node() {
        for (int i = 0; i < next.size(); i++)
            delete next[i];
    }
};
Run Code Online (Sandbox Code Playgroud)

(我知道把它变成一个类可能会更好,但让我们考虑一下它).

我不太确定我是否为此编写了正确的析构函数.在main函数中我使用的是根节点:

Node *root = new Node();
Run Code Online (Sandbox Code Playgroud)

c++ destructor

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

删除所有具有零值的地图键

有这个地图,其键属于{0,1,2,3}.

我需要擦除其值等于0的所有键.

这段代码是一个好习惯吗?

map<int, int> nums = {{0, 1}, {1, 3}, {2, 0}, {3, 1}};

for(int i = 0; i < 4; i++)
    if (nums.count(i) > 0 && nums[i] == 0)
        nums.erase(i);
Run Code Online (Sandbox Code Playgroud)

它似乎工作但迭代在地图上并在同一循环中擦除键让我感到不舒服.

如果这段代码不是很好的方式,那么在地图中删除零值的所有键的最佳方法是什么?

c++ dictionary erase

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

为什么“没有可行的运算符重载”?

我正在编写一个二维数组类并尝试重载运算符 []:

typedef unsigned long long int dim;

template<typename N>
class Array2D {

private:

    dim _n_rows;
    dim _n_cols;
    vector<vector<N>> M;

public:

    dim n_rows() { return _n_rows; }
    dim n_cols() { return _n_cols; }
    Array2D(): _n_rows(0), _n_cols(0), M(0, vector<N>(0)){}
    Array2D (const dim &r, const dim &c) : _n_rows(r), _n_cols(c), M(r, vector<N>(c)) {}

    void set(const dim &i, const dim &j, const N &elem) { M[i][j] = elem; } // Works fine
    vector<N>& operator[](int &index) { return M[index]; } // <- PROBLEM
}; …
Run Code Online (Sandbox Code Playgroud)

c++

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