找到 34423 篇文章 关于编程

在 C++ 中检查双精度浮点数(或浮点数)是否为 NaN

Nishtha Thakur
更新于 2019-07-30 22:30:25

13K+ 次浏览

要检查浮点数或双精度浮点数是否为 NaN(非数字)在 C++ 中,我们可以使用 isnan() 函数。isnan() 函数存在于 cmath 库中。此函数在 C++ 版本 11 中引入。因此,从 C++11 开始,我们可以使用此函数。示例#include #include using namespace std; main() {    if(isnan(sqrt(30))) { //30 的平方根是一个浮点数       cout

C++ 中内联函数的优点是什么?

Anvi Jain
更新于 2019-07-30 22:30:25

813 次浏览

C++ 内联函数是一个强大的概念,通常与类一起使用。如果一个函数是内联的,则编译器会在编译时将该函数代码的副本放置在每个调用该函数的位置。对内联函数的任何更改都可能需要重新编译该函数的所有客户端,因为编译器需要再次替换所有代码,否则它将继续使用旧的功能。要内联一个函数,请在函数名称之前放置关键字 inline 并定义函数在任何对该函数的调用之前。编译器可以忽略 ... 阅读更多

如何在 gcc 中从 C/C++ 源代码获取汇编程序输出?

Smita Kapse
更新于 2019-07-30 22:30:25

2K+ 次浏览

在这里,我们将了解如何使用 gcc 从 C 或 C++ 代码生成汇编程序输出。gcc 提供了一个很棒的功能,可以在执行源代码时获取所有中间输出。要获取汇编程序输出,我们可以为 gcc 使用选项“-S”。此选项显示编译后的输出,但在发送到汇编程序之前。此命令的语法如下所示。gcc –S program.cpp现在,让我们看看输出将是什么样子。这里我们使用一个简单的程序。在这个程序中,两个数字存储在变量 x 和 y 中,然后 ... 阅读更多

C++ 函数参数的求值顺序

Nishtha Thakur
更新于 2019-07-30 22:30:25

156 次浏览

我们将不同的参数传递给一些函数。现在我们可能会想到一个问题,那就是函数参数的求值顺序是什么。是从左到右还是从右到左?为了检查求值顺序,我们将使用一个简单的程序。这里传递了一些参数。从输出中,我们可以找到它们是如何求值的。示例 实时演示#include using namespace std; void test_function(int x, int y, int z) {    cout

Java 程序从字符串/字节数组创建流

Krantik Chavan
更新于 2019-07-30 22:30:25

485 次浏览

创建输入流并设置字符串:DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));getBytes() 方法用于将字符串转换为字节序列并返回一个字节数组。现在返回一个输入字节:(char) inputStream.readByte()示例import java.io.ByteArrayInputStream; import java.io.DataInputStream; public class Demo { public static void main(String[] args) throws Exception {    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));       System.out.print((char) inputStream.readByte());       System.out.print((char) inputStream.readByte());       inputStream.close();    } }输出Pq

如何在 Java 中使用自定义比较器对数组进行排序?

Krantik Chavan
更新于 2019-07-30 22:30:25

2K+ 次浏览

假设以下为我们的字符串数组,我们需要对其进行排序:String[] str = { "Tom", "Jack", "Harry", "Zen", "Tim", "David" };在 sort() 方法中,创建一个自定义比较器对上述字符串进行排序。在这里,两个字符串相互比较,并且过程继续:Arrays.sort(str, new Comparator < String > () {    public int compare(String one, String two) {       int val = two.length() - one.length();       if (val == 0)       val = one.compareToIgnoreCase(two);       return val;    } });示例import java.util.Arrays; import java.util.Comparator; public class Demo ... 阅读更多

Java 程序获取两个时间点之间的时间差

Krantik Chavan
更新于 2019-07-30 22:30:25

238 次浏览

创建两个时间点:Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50);使用 between() 获取两个时间点之间的时间差:long resMilli = Duration.between(time1, time2).toMillis();示例import java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant time1 = Instant.now();       Instant time2 = Instant.now().plusSeconds(50);       long resMilli = Duration.between(time1, time2).toMillis();       System.out.println("两个时间间隔之间的时间差 = "+resMilli);    } }输出两个时间间隔之间的时间差 = 50000

Java 程序从 Clock 创建 LocalDateTime

Nancy Den
更新于 2019-07-30 22:30:25

356 次浏览

首先,设置 Clock:Clock clock = Clock.systemUTC();现在,创建 LocalDateTime:LocalDateTime dateTime = LocalDateTime.now(clock);示例import java.time.Clock; import java.time.LocalDateTime; public class Demo {    public static void main(String[] args) {       Clock clock = Clock.systemUTC();       System.out.println("Clock = "+Clock.systemDefaultZone());       LocalDateTime dateTime = LocalDateTime.now(clock);       System.out.println("LocalDateTime = "+dateTime);    } }输出Clock = SystemClock[Asia/Calcutta] LocalDateTime = 2019-04-19T09:29:50.605820900

Java 程序将 LocalTimeDate 格式化为 BASIC_ISO_DATE 格式

Nancy Den
更新于 2019-07-30 22:30:25

404 次浏览

首先,设置日期:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10);现在,将日期时间格式化为 BASIC_ISO_DATE 格式:String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);示例import java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);       System.out.println("格式化后的日期 = "+str);    } }输出DateTime = 2019-09-06T20:10 格式化后的日期 = 20190906

Java 程序按字母顺序对数组进行排序

Nancy Den
更新于 2019-07-30 22:30:25

659 次浏览

让我们首先创建一个字符串数组:String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };现在,使用 Arrays.sort() 方法按字母顺序对数组进行排序。在这里,我们已将顺序设置为不区分大小写:Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);示例import java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };       System.out.println("按字母顺序排序数组...");       Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);       for (int a = 0; a < strArr.length; a++) {          System.out.println(strArr[a]); ... 阅读更多

广告

© . All rights reserved.