# Programming Perl--Column1

problem：

input：一个至多包含n=10,000,000个正整数的file，所有的integer都必须小于n，integer不允许重复出现

output：升序排序的integer list

solution：

/* phase 1: initialize set to empty */

for i = [0,n)

bit[i] = 0

/* phase 2: insert present elements into the set */

for each i in the input file

bit[i] = 1

/* phase  3: write sorted output */

for i = [0,n)

if bit[i] == 1

write i on the output file

1、如果没有memory限制时，代码如何写

#include <iostream>
#include <set>

int main (int argc, char *argv[]) {
std::set<int> integerSet;

int i;

std::set<int>::iterator iter;
while (std::cin >> i) {
integerSet.insert(i);
}

for (iter = integerSet.begin(); iter != integerSet.end(); ++iter) {
std::cout << *iter << " ";
}
std::cout << std::endl;

return 0;
}

Set

Sets are a kind of associative containers that stores unique elements, and in which the elements themselves are thekeys.

Associative containers are containers especially designed to be efficient accessing its elements by their key (unlike sequence containers, which are more efficient accessing elements by their relative or absolute position).

Internally, the elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction.

Sets are typically implemented as binary search trees.

Therefore, the main characteristics of set as an associative container are:

• Unique element values: no two elements in the set can compare equal to each other. For a similar associative container allowing for multiple equivalent elements, seemultiset.
• The element value is the key itself. For a similar associative container where elements are accessed using a key, but map to a value different than this key, seemap.
• Elements follow a strict weak ordering at all times. Unordered associative arrays, likeunordered_set, are available in implementations following TR1.

This container class supports bidirectional iterators.

List

Lists are a kind of sequence containers. As such, their elements are ordered following a linear sequence.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the element preceding it and a link to the element following it.

This provides the following advantages to list containers:

• Efficient insertion and removal of elements anywhere in the container (constant time).
• Efficient moving elements and block of elements within the container or even between different containers (constant time).
• Iterating over the elements in forward or reverse order (linear time).

Compared to other base standard sequence containers (vectors anddeques), lists perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in alist one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

Storage is handled automatically by the class, allowing lists to be expanded and contracted as needed.

list采用双向链表的方式实现的，这对于频繁进行插入删除操作比较有利，但对于本问题而言就有些声东击西了，但是同时也可以看到list也是有序的

Vector

Vectors are a kind of sequence containers. As such, their elements are ordered following a strict linear sequence.

Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

But unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed.

Vectors are good at:

• Accessing individual elements by their position index (constant time).
• Iterating over the elements in any order (linear time).
• Add and remove elements from its end (constant amortized time).

Compared to arrays, they provide almost the same performance for these tasks, plus they have the ability to be easily resized. Although, they usually consume more memory than arrays when their capacity is handled automatically (this is in order to accomodate for extra storage space for future growth).

vector的优点或者说突出点在于动态内存空间分配。

2、使用位操作符实现bitset操作

#include <iostream>

#define MAX_LENGTH 10000000
#define INT_LENGTH 32
#define SHIFT 5

int32_t integerArray[1 + MAX_LENGTH >> SHIFT];

void set(int32_t i){
integerArray[i >> SHIFT] |= (1 << (i & MASK));
}

void clear(int32_t i) {
integerArray[i >> SHIFT] &= ~(1 << (i & MASK));
}

int test(int32_t i) {
return integerArray[i >> SHIFT] & (1 << (i & MASK));
}

int main (int argc, char *argv[]) {
for (int32_t i = 0; i < 10000000; i++) {
clear(i);
}
int32_t i;
while (std::cin >> i) {
set(i);
if (test(i)) {
std::cout << i << " is set" << std::endl;
}
}

return 0;
}

• 本文已收录于以下专栏：

## column_layout_grid_for_product_listing_1-3-8

• 2013年06月04日 13:53
• 12KB
• 下载

## amchart -1 Column &amp; Bar(flash chart 图表)

• 2010年01月02日 16:50
• 327KB
• 下载

## 20 Killer Perl Programming Tips for Beginners on UNIX / Linux OS

1. List all Installed Perl Modules from Unix Command Line \$ perl -MFile::Find=find -MFile::Spec::Fu...

## 第二十七章 Introduction to Perl Programming（中文版）

Perl基础 脚本 Perl 是一种脚本语言，它在每次运行前都需要编译。在unix/linux下，通常perl脚本都是以 #!/usr/bin/perl 为开头的。这其实是perl（可执行文件...
• maidanm
• 2012年08月11日 03:18
• 1113

>第七章、POE(2) 原文链接: http://blog.yikuyiku.com/?p=1362 1. Wheels Wheels就是POE的I/O系统的动力之源。 wheel帮助...

## Usefunctional programming in Perl to make test automation code more structural

Abstract In USD test automation, we used Perl as the majorprogramming language. With daily practice...

## 【报错】BatchUpdateException: Data truncated for column 'id' at row 1

【报错】 log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry). l...

## MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题以及error 1406:data too long for column 'name' at row 1

MySQL中涉及的几个字符集 character-set-server/default-character-set：服务器字符集，默认情况下所采用的。 character-set-databas...

## 【Hibernate错误解决办法】Could not execute JDBC batch update； Data too long for column 'name' at row 1

举报原因： 您举报文章：Programming Perl--Column1 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)