小编JAN*_*JAN的帖子

对于单行if或循环使用大括号(即{})的目的是什么?

我正在阅读我的C++讲师的一些讲义,他写了以下内容:

  1. 使用缩进//确定
  2. 永远不要依赖运算符优先级 - 始终使用括号//确定
  3. 总是使用{}块 - 即使是单行// 不行,为什么???
  4. 比较左侧的Const对象// OK
  5. 对于> = 0 //好玩法的变量使用无符号
  6. 删除后将指针设置为NULL - 双删除保护//不错

第三种技术对我来说并不清楚:通过在一条线中放置一条线可以获得{ ... }什么?

例如,拿这个奇怪的代码:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
{
    if (i % 2 == 0)
    {
        j++;
    }
}
Run Code Online (Sandbox Code Playgroud)

并替换为:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
    if (i % 2 == 0)
        j++;
Run Code Online (Sandbox Code Playgroud)

使用第一个版本有什么好处?

c c++ defensive-programming coding-style curly-braces

313
推荐指数
12
解决办法
3万
查看次数

dup2/dup - 为什么我需要复制文件描述符?

我想了解使用dup2dup.

从手册页:

DESCRIPTION

dup and dup2 create a copy of the file descriptor oldfd.
After successful return of dup or dup2, the old and new descriptors may
be used interchangeably. They share locks, file position pointers and
flags; for example, if the file position is modified by using lseek on
one of the descriptors, the position is also changed for the other.

The two descriptors do not share the close-on-exec flag, however.

dup uses the lowest-numbered …
Run Code Online (Sandbox Code Playgroud)

c linux operating-system system-calls

79
推荐指数
2
解决办法
4万
查看次数

O(n)和O(log(n))之间的差异 - 哪个更好,什么是O(log(n))?

这是我在数据结构和每个讲座/ TA讲座的第一门课程,我们谈论O(log(n)).这可能是一个愚蠢的问题,但如果有人能够向我解释它究竟是什么意思,我会很感激!

algorithm complexity-theory big-o logarithm data-structures

55
推荐指数
5
解决办法
5万
查看次数

int main()和int main(void)之间的区别?

以下是什么意思:

int main(void) {...} 
Run Code Online (Sandbox Code Playgroud)

VS

int main() {...}
Run Code Online (Sandbox Code Playgroud)

我认为这int main() {...}意味着main不接收任何参数(来自命令行),但是:

int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)

确实.

但这int main(void) {...} 意味着什么?什么是无效主张?

我看过这里,但这是一个不同的问题.

c program-entry-point void

55
推荐指数
3
解决办法
7万
查看次数

java.sql.SQLException:对用户'root'@'localhost'拒绝访问(使用密码:YES)

以下代码:

Class.forName("com.mysql.jdbc.Driver");
Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost","root","root");
Run Code Online (Sandbox Code Playgroud)

抛出此异常getConnection():

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at db.Database.<init>(Database.java:91)
    at db.Main.main(Main.java:10)
Run Code Online (Sandbox Code Playgroud)

这是怎么造成的,我该如何解决?

编辑:

    public static void main(String[] args) throws ClassNotFoundException, ServletException, SQLException 
    { …
Run Code Online (Sandbox Code Playgroud)

java mysql connection exception jdbc

41
推荐指数
7
解决办法
30万
查看次数

org.hibernate.MappingException:未知实体:annotations.Users

考虑层次结构:

在此输入图像描述

以下类和xml:

HibernateUtil.java

package annotations;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


/**
 * 
 * @author X3
 *
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static final String HIBERNATE_CFG = "hibernateAnnotations.cfg.xml";

    private static SessionFactory buildSessionFactory() 
    {
        Configuration cfg = new Configuration().addResource(HIBERNATE_CFG).configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                applySettings(cfg.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
Run Code Online (Sandbox Code Playgroud)

Users.java

package annotations;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import …
Run Code Online (Sandbox Code Playgroud)

java entity hibernate exception

39
推荐指数
6
解决办法
18万
查看次数

运算符"点"(.)是什么意思?

鉴于代码:

 A = [1 2 3; 3 2 1]
 B = A.^2
Run Code Online (Sandbox Code Playgroud)

输出 :

B =

     1     4     9
     9     4     1
Run Code Online (Sandbox Code Playgroud)

但如果我这样做: B = A^2

输出是:

Error using  ^ 
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Run Code Online (Sandbox Code Playgroud)

操作员.究竟做了什么?

syntax matlab operators

31
推荐指数
2
解决办法
11万
查看次数

如何设置errno值?

我有两种不同方法的调用:

void func1() 
{
  // do something 
  if (fail) 
  {
    // then set errno to EEXIST

  }

}
Run Code Online (Sandbox Code Playgroud)

第二种方法:

void func2() 
{
  // do something 
  if (fail) 
  {
    // then set errno to ENOENT

  }

}
Run Code Online (Sandbox Code Playgroud)
  1. 当我设置errno为某个值时,它会做什么?只是错误检查?

  2. 我怎样才能设置errno在上面的方法func1,并func2EEXISTENOENT

谢谢

c linux error-handling errno

29
推荐指数
3
解决办法
4万
查看次数

如何验证字符串数组是否包含某个字符串?

鉴于:

String[] directions = {"UP","DOWN","RIGHT","LEFT","up","down","right","left"};

String input = "Up";
Run Code Online (Sandbox Code Playgroud)

如何验证输入stdin是否在directions数组内?

我可以制作一个循环并input使用相同的方式检查每个项目,但我正在寻找一种更优雅的方式.

问候,罗恩

java arrays string compare

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

提取2个数字n次并将添加放回O(n)而不是O(n*log(n))

我提出了我的教授在课堂上展示的问题,我的O(n*log(n))解决方案:

给出一个n我们想要执行以下n-1时间的数字列表:

  • x,y从列表中提取两个最小元素并显示它们
  • 创建一个新号码z,在哪里z = x+y
  • z回列表

O(n*log(n))和建议数据结构和算法O(n)

解:

我们将使用最小堆:

仅创建一次堆就需要O(n).之后,提取两个最小元素将采用O(log(n)).放置z到堆中将采取为O(log(n))的.

执行上述n-1时间需要O(n*log(n)),因为:

O(n)+O(n?(logn+logn ))=O(n)+O(n?logn )=O(n?logn )
Run Code Online (Sandbox Code Playgroud)

但我怎么能在O(n)中做到这一点?

编辑:

通过说:" x,y从列表中提取两个最小元素并呈现它们 ",我的意思是printf("%d,%d" , x,y),当前列表中的最小元素xy位置.

arrays algorithm math list data-structures

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