Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
函数
|
127
函数
对于有
n
个元素的数组来说,二元搜索算法进行最多
1+log
2
(
n
)
次比较。如果有一百万个元
素,最多比较
20
次,也就是最多
20
次递归执行函数
binarysearch()
。
递归函数基于了这样一个事实:每次调用函数时,都会重新建立动态变量。这些变量,
以及返回时需要的调用者地址,都存储在栈中,每次函数递归都会造成栈上新增一块数
据。程序员要确保栈的空间够大,足以容纳递归的中间处理过程。不过例
7-8
中的函数
binarySearch()
对于栈空间的要求并不大。
递归函数是采用逻辑方式来实现自然递归规律的算法,例如二元搜索技术,或者树状结
构导航(
navigation
)。然而,即使递归函数可以对某些问题提供优雅而紧凑的解决方
案,但采用简单循环的解决方案也常常是可行的。例如,可以用循环改写例
7-8
的二元搜
索算法,而不使用递归函数调用。在这种情况下,采用循环的解决方案通常会比采用递
归的解决方案执行效率更高。
7.8 可变参数函数
C
语言允许定义参数数量可变的函数,这称为可变参数函数(
variadic function
)。这
种函数需要固定数量的强制参数(
mandatory argument
),后面是数量可变的可选参数
(
optional argument
)。这种函数必须至少有一个强制参数。可选参数的类型可以变
化。可选参数的数量由强制参数的值决定,或由用来定义可选参数列表的特殊值决定。
C
语言中最常用的可变参数函数例子是
printf()
和
scanf()
。这两个函数都有一个强制参
数,即格式化字符串。格式化字符串中的转换修饰符决定了可选参数的数量和类型。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207