memset

Using memset for integer array in c

萝らか妹 提交于 2019-11-26 18:46:00
char str[]="beautiful earth"; memset(str,'*',6); printf("%s",str); output: ******ful earth 1) Like above use of memset, can we initialize only few integer array index values to 1 as given below?? int arr[15]; memset(arr,1,6); No, you cannot use memset() like this. The manpage says (emphasis mine): The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c . Since an int is usually 4 bytes, this won't cut it. If you ( incorrectly!! ) try to do this: int arr[15]; memset(arr, 1, 6*sizeof(int)); //wrong! then the first 6 int s in the array will

HDU3896 Greatest TC

那年仲夏 提交于 2019-11-26 18:14:21
Problem Description TC (Tian Chao) is magical place, as you all know... The railways and the rail-stations in TC are fragile and always meet with different kinds of problems. In order to reach the destination safely on time, you are asked to develop a system which has two types of main functions as below. 1: A B C D, reporting whether we can get from station A to station B without passing the railway that connects station C and station D. 2: A B C, reporting whether we can get from station A to station B without passing station C. Please notice that the railways are UNDIRECTED. Input For each

经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)

落爺英雄遲暮 提交于 2019-11-26 18:12:59
题目来源 P3376 【模板】网络最大流 P2756 飞行员配对方案问题 P3381 【模板】最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建反向边,这样相当于给了反悔的机会,不断地求增广路的,最终得到最大流 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<string> #include<fstream> #include<vector> #include<stack> #include <map> #include <iomanip> #define bug cout << "**********" << endl #define show(x,y) cout<<"["<<x<<","<<y<<"] " //#define LOCAL = 1; using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const ll mod = 1e6 + 3; const int Max = 1e5 + 10; struct Edge { int to, next, flow; /

关于memset的赋值(最大值最小值的选择)

孤者浪人 提交于 2019-11-26 18:02:09
memset赋值赋的是ASSCII码转为二进制赋值 比如 memset(,0xff,sizeof()),0xff转为二进制11111111,int为4字节所以最后为11111111111111111111111111111111为-1。(化为二进制补位,然后再赋值)。 而OIER通常都希望能够通过memset赋给数组一个最大值 ## 如何定义这个无穷大 这个主要还是看数据范围。 如果直接用int最大值或者和最大值同位数的值作为无穷大的话 比如 2147483647 + 999 爆了int , 它就会从我们定义的无穷大变成负无穷大,这不满足我们的期望。 所以到底该用什么来当这个 —— 无穷大 经常会看到大佬啊神犇啊会用: memset( , 0x3f , sizeof ); 特意去试了下,发现 0x3f3f3f3f 真的是个非常精巧的常量 他的十进制是 1061109567也就是10^9级别的(和0x7fffffff一个数量级),作为一个oier, 一般场合下的题目数据都是小于10^9的,所以它可以作为无穷大使用而不致出现数据大于无穷大的情形 因为0x3f3f3f3f的每个字节都是0x3f!所以要把一段内存全部置为无穷大,我们只需要memset(a,0x3f,sizeof(a))(memset按字节赋值)。 所以在通常的场合下,0x3f3f3f3f真的是一个非常棒的选择。

Is it safe to memset bool to 0?

若如初见. 提交于 2019-11-26 17:46:45
问题 Suppose I have some legacy code which cannot be changed unless a bug is discovered, and it contains this code: bool data[32]; memset(data, 0, sizeof(data)); Is this a safe way to set all bool in the array to a false value? More generally, is it safe to memset a bool to 0 in order to make its value false ? Is it guaranteed to work on all compilers? Or do I to request a fix? 回答1: I believe this unspecified although it seems likely the underlying representation of false would be all zeros. Boost

Should C++ programmer avoid memset?

心不动则不痛 提交于 2019-11-26 15:52:56
问题 I heard a saying that c++ programmers should avoid memset, class ArrInit { //! int a[1024] = { 0 }; int a[1024]; public: ArrInit() { memset(a, 0, 1024 * sizeof(int)); } }; so considering the code above,if you do not use memset,how could you make a[1..1024] filled with zero?Whats wrong with memset in C++? thanks. 回答1: The issue is not so much using memset() on the built-in types, it is using them on class (aka non-POD) types. Doing so will almost always do the wrong thing and frequently do the

Tarjan算法(lca)

隐身守侯 提交于 2019-11-26 14:31:04
http://codevs.cn/problem/2370 / 2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上。有一天,他们想爬到一个节点上去搞基,但是作为两只虫子,他们不想花费太多精力。已知从某个节点爬到其父亲节点要花费 c 的能量(从父亲节点爬到此节点也相同),他们想找出一条花费精力最短的路,以使得搞基的时候精力旺盛,他们找到你要你设计一个程序来找到这条路,要求你告诉他们最少需要花费多少精力 输入描述 Input Description 第一行一个n,接下来n-1行每一行有三个整数u,v, c 。表示节点 u 爬到节点 v 需要花费 c 的精力。第n+1行有一个整数m表示有m次询问。接下来m行每一行有两个整数 u ,v 表示两只虫子所在的节点 输出描述 Output Description 一共有m行,每一行一个整数,表示对于该次询问所得出的最短距离。 样例输入 Sample Input 3 1 0 1 2 0 1 3 1 0 2 0 1 2 样例输出 Sample Output 1 1 2 数据范围及提示 Data Size & Hint 1<=n<=50000, 1<

memset初始化数组的坑

强颜欢笑 提交于 2019-11-26 13:56:45
memset 函数常被我们用来初始化数组,然而有个坑可能会被我们踩到。 静态数组初始化 一般情形是这样的: #include <cstring> int main() { // 静态数组arr int arr[10]; // 将数组所有元素初始化为0 memset(arr,0,sizeof(arr)); // 遍历输出数组元素 for(int i=0;i<10;++i){ cout << arr[i] << endl; } return 0; } 这样做是正确的,通过输出可以看到正确结果。 注意:这份代码中 arr 是个静态数组。 动态数组初始化 #include <cstring> int main() { // 动态数组arr int* arr=new int[10]; // 将数组所有元素初始化为0 memset(arr,0,sizeof(arr)); // 遍历输出数组元素 for(int i=0;i<10;++i){ cout << arr[i] << endl; } return 0; } 这份代码运行后,我们发现 memset 并没有把数组元素全部初始化为0。 注意: arr 是个动态数组 原因如下 : 我们可以在上面两份代码中输出 sizeof(arr) ,可以发现结果是不一样的。 因为在第一份代码中 arr 代表一个数组,第二份代码中 arr 代表一个指针。(关于

What is the equivalent of memset in C#?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 12:12:28
I need to fill a byte[] with a single non-zero value. How can I do this in C# without looping through each byte in the array? Update: The comments seem to have split this into two questions - Is there a Framework method to fill a byte[] that might be akin to memset What is the most efficient way to do it when we are dealing with a very large array? I totally agree that using a simple loop works just fine, as Eric and others have pointed out. The point of the question was to see if I could learn something new about C# :) I think Juliet's method for a Parallel operation should be even faster

Why is memset() incorrectly initializing int?

▼魔方 西西 提交于 2019-11-26 09:09:57
问题 Why is the output of the following program 84215045 ? int grid[110]; int main() { memset(grid, 5, 100 * sizeof(int)); printf(\"%d\", grid[0]); return 0; } 回答1: memset sets each byte of the destination buffer to the specified value. On your system, an int is four bytes, each of which is 5 after the call to memset . Thus, grid[0] has the value 0x05050505 (hexadecimal), which is 84215045 in decimal. Some platforms provide alternative APIs to memset that write wider patterns to the destination