getline

牛客 C++刷题day22

血红的双手。 提交于 2019-11-30 10:04:24
switch 语句中执行顺序: 如果某一个case中不加break,那么假如某一次切换到该case则按照语句顺序,执行接下来的所有case句子,直到遇到break,注意,switch语句中的语句顺序非常重要。 原子操作( Atomic operations)不可中断的一个或一系列操作。 标准库里面的string在多线程下并不保证是都是安全的,只提供两种安全机制: 1.多个线程同时读取数据是安全的。 2.只有一个线程在写数据是安全的。 局部变量局部使用是安全的 为什么?因为每个thread 都有自己的运行堆栈,而局部变量是生存在堆栈中,大家不干扰。 2.全局原生变量多线程读写是不安全的 , 全局变量是在堆(heap)中。 3.函数静态变量多线程读写也是不安全的。 4.volatile能保证全局整形变量是多线程安全的么? 不能。 volatile仅仅是告诫compiler不要对这个变量作优化,每次都要从memory取数值,而不是从register 5.InterlockedIncrement保证整型变量自增的原子性 写好多线程安全的法宝就是封装,使数据有保护的被访问到 安全性: 局部变量 > 成员变量 > 全局变 函数的隐含储存类型是extern,函数的形参或变量的储存类型为auto Cin的用法 1.cin简介 cin是C++编程语言中的标准输入流对象,即istream类的对象

mmap slower than getline?

亡梦爱人 提交于 2019-11-30 05:27:26
I face the challenge of reading/writing files (in Gigs) line by line. Reading many forum entries and sites (including a bunch of SO's), mmap was suggested as the fastest option to read/write files. However, when I implement my code with both readline and mmap techniques, mmap is the slower of the two. This is true for both reading and writing. I have been testing with files ~600 MB large. My implementations parse line by line and then tokenize the line. I will present file input only. Here is the getline implementation: void two(char* path) { std::ios::sync_with_stdio(false); ifstream pFile

gawk / awk: piping date to getline *sometimes* won't work

杀马特。学长 韩版系。学妹 提交于 2019-11-30 05:12:21
问题 I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the variable supposed to hold the (converted) date remains undefined. This always happens with the exact same rows. Running `date' explicitly (from the Bash shell) on the dates of those weird rows works fine (the dates are properly converted). -- It's

C++ getline函数用法详解

て烟熏妆下的殇ゞ 提交于 2019-11-30 02:15:22
虽然可以使用 cin 和 >> 运算符来输入字符串,但它可能会导致一些需要注意的问题。 当 cin 读取数据时,它会传递并忽略任何前导白色空格字符(空格、制表符或换行符)。一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时,它将停止读取。以下面的语句为例: cin >> namel; 可以输入 "Mark" 或 "Twain",但不能输入 "Mark Twain",因为 cin 不能输入包含嵌入空格的字符串。下面程序演示了这个问题: 复制纯文本复制 // This program illustrates a problem that can occur if// cin is used to read character data into a string object.#include <iostream>#include <string> // Header file needed to use string objectsusing namespace std; int main(){ string name; string city; cout << "Please enter your name: "; cin >> name; cout << "Enter the city you live in: "; cin >> city; cout <<

getline 函数

故事扮演 提交于 2019-11-30 00:55:37
#include <bits/stdc++.h> #include <string> using namespace std; string s; int main() { getline(cin,s); cout<<s<<endl; /* fsw sf fsw sf */ cin>>s cout<<s<<endl; fsw sf fsw return 0; } 来源: https://www.cnblogs.com/tingtin/p/11542396.html

Use getline and >> when read file C++

夙愿已清 提交于 2019-11-29 18:10:24
Because data from file look like this: line 1 is name (first last), next line is score (score1 score 2 ....score5) and so on... So I think that I need getline for name and >> for score Example of data file David Beckham 80 90 100 20 50 Ronaldinho Gaucho 99 80 100 20 60 .... First of all, I have structure struct Player { string name; int score[5]; } player[size] When read data from file int i = 0; while(!file.eof()) { for (int j = 0; j < 2; j++) //read each 2 two lines { if(j==0) // name { getline(file, player[i].name); } else if(j==1) // score { for(int k=0; k<5; k++) file >> player[i].grade[k

Coding a getline() implementation - Valgrind errors

隐身守侯 提交于 2019-11-29 18:02:30
I have to recode an implementation of the getline() function, but using the file descriptor of the file and not a FILE * . I am only allowed to use malloc() and free() , along with 5 functions being 25 lines long at most. I think I've done correctly the project although I am a beginner in C and my code isn't probably good. When I run it, it works fine, but valgrind shows that I definetely lost x bytes , x depending of the file length and the READ_SIZE (macro defined in the header). According to valgrind's --leak-check=full , I have a memory leak in the str_realloc_cat function, when I malloc

Reading multiple lines from a file using getline()

有些话、适合烂在心里 提交于 2019-11-29 14:17:34
I am trying to read in and then output the contents of a text file with three lines, as follows: Bob Dylan 10 9 John Lennon 8 7 David Bowie 6 5 For each line, I just want to output the line, i.e. firstName LastName number1 number2. I'm using the following code for this: int num1; int num2; string firstName; string lastName; string fullName; ifstream inFile; inFile.open("inputFile.txt"); while (getline(inFile, firstName)) { inFile >> firstName >> lastName >> num1 >> num2; fullName = firstName + " " + lastName; cout << fullName << " " << num1 << " " << num2 << endl; } inFile.close(); There are 2

Program is skipping over Getline() without taking user input [duplicate]

偶尔善良 提交于 2019-11-29 11:56:56
This question already has an answer here: Why does std::getline() skip input after a formatted extraction? 3 answers This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) { case 'Y': Entrynumber++; cout << "began record number " << Entrynumber << "+ 1." << endl; cout << "Enter the last name of the person to be entered" <<

difference between cin.get() and cin.getline()

两盒软妹~` 提交于 2019-11-29 10:54:51
I am new to programming, and I have some questions on get() and getline() functions in C++. My understanding for the two functions: The getline() function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue. The book(C++ Primer Plus) that I am reading is suggesting using get() over getline() . My confusion is that isn't getline() safer than get() since it makes sure to end line with '\n' . On the