我读过C++ Streams和C风格的IO?(以及其他页面)试图帮助我决定在我正在进行的项目中实现某些文件IO的方法.
背景 我是C++和Windows编程的新手,我一直在C和命令行应用程序中工作.对这个问题的n00b提前道歉.
问题 我想读取一个文本文件,处理内容并输出到另一个(新)文本文件.我在Win32环境中工作(这对于可预见的未来不会改变)并且正在通过_T样式宏编写应用程序以识别Unicode."处理"可以包括插入/附加/删除文本行,最多128个字符.
这个问题 我更喜欢写一些强大的东西,因此I/O错误处理是一个考虑因素.我认为我需要远离C风格文件I/O,除非简化代码和类型检查没有其他原因 - 即在更多的OO POV中处理这个问题.使用Win32 API函数而不是C++流函数(如果有的话)有什么好处?你能为这两种方法推荐一个好的底漆吗?(我的谷歌搜索给我留下了一点信息超载)
非常感谢
我需要将 String(这是一个有效的整数)与 int 值进行比较。
对于String str, int integer,我的选择是:
Integer.parseInt(str) == integer
str.equals(Integer.toString(integer))
哪一种更快,有没有更好的方法?
以下受到 atlaste 答案的 EqualsIntString 的启发
private static boolean checkEquality(final String string, final long value) {
if (string == null) {
return false;
}
final int length = string.length();
if (length == 0) {
return false;
}
long absValue;
final byte minIndex;
if (string.charAt(0) == '-') {
if (value > 0) {
return false;
}
absValue = -value;
minIndex = 1;
} else {
if …Run Code Online (Sandbox Code Playgroud) 我正在执行有关C ++中堆栈的数据结构编程任务。
在此作业中,我应该读取很多整数(在最坏的情况下,我应该读取1,600,000个整数),最后输出一些字符串。
作为一名学生,我提交了cpp源文件,然后网站对我的源代码进行了评估和评分。我得到了100%,但我想做得更好。此分配的时间限制为2秒,我的源代码的执行时间为128毫秒。但是,成绩最好的学生仅用52毫秒即可完成任务。所以我想知道如何使我的代码更快。
我的源代码主要包含三个部分:
OnlineJudge告诉我代码的执行时间。第一部分花费100毫秒,第二部分花费20毫秒,而第三部分花费12毫秒。因此,如果我想使代码更快,则应该提高输入速度。
OnlineJudge的输入是这样的:
5 2
1 2 3 5 4
Run Code Online (Sandbox Code Playgroud)
第一行是两个整数n和m,第二行是n个由空格分隔的整数。限制为:1 <= n <= 1,600,000和0 <= m <= 1,600,000。为了读取超过一百万个整数,我的代码是这样的:
#include <iostream>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int *exit = new int[1600000];
cin>>n>>m;
for (int i=0;i<n;++i)
cin>>exit[i];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果n小,则OnlineJudge会说执行时间为0毫秒。如果n非常大,例如1,600,000。OnlineJudge说此代码需要100毫秒。如果我删除
std::ios::sync_with_stdio(false);
cin.tie(NULL);
Run Code Online (Sandbox Code Playgroud)
然后代码需要424毫秒。但是,在此作业中必须读取整数,因此我很好奇顶尖的学生如何在52毫秒内完成“ cin,查找解决方案,退出”。
您对提高输入速度有任何想法吗?
2019.4.17?有人建议使用vector或std :: from_chars,但在此分配中这些被禁止。如果我写
#include <vector>
Run Code Online (Sandbox Code Playgroud)
要么
#include <charconv>
Run Code Online (Sandbox Code Playgroud)
要么
#include <array>
Run Code Online (Sandbox Code Playgroud)
然后OnlineJudge说“编译错误”。
有人建议使用scanf,我的代码如下:
for (int i=0;i<n;++i)
scanf("%d", &exit[i]);
Run Code Online (Sandbox Code Playgroud)
但是执行时间是120毫秒。顺便说一句,我不认为scanf比cin快,在C ++程序中使用scanf()比与cin快?
有人建议使用getline。我很少使用此功能,我的代码如下:
stringstream ss;
string …Run Code Online (Sandbox Code Playgroud) 我正在研究一个从文本文件中读取数百万个浮点数的程序.这个程序在我正在设计的游戏中运行,所以我需要它快速(我正在加载一个obj文件).到目前为止,由于Convert.ToSingle()的速度较慢,加载一个相对较小的文件大约需要一分钟(没有预编译).有更快的方法吗?
编辑:这是我用来解析Obj文件的代码
using System;
using System.IO;
using System.Collections.Generic;
using OpenTK.Math;
using System.Drawing;
using PlatformLib;
public class ObjMeshLoader
{
public static StreamReader[] LoadMeshes(string fileName)
{
StreamReader mreader = new StreamReader(PlatformLib.Platform.openFile(fileName));
MemoryStream current = null;
List<MemoryStream> mstreams = new List<MemoryStream>();
StreamWriter mwriter = null;
if (!mreader.ReadLine().Contains("#"))
{
mreader.BaseStream.Close();
throw new Exception("Invalid header");
}
while (!mreader.EndOfStream)
{
string cmd = mreader.ReadLine();
string line = cmd;
line = line.Trim(splitCharacters);
line = line.Replace(" ", " ");
string[] parameters = line.Split(splitCharacters);
if (parameters[0] == "mtllib") …Run Code Online (Sandbox Code Playgroud) 2008-11-28 每日WTF以下代码:
static char *nice_num(long n)
{
int neg = 0, d = 3;
char *buffer = prtbuf;
int bufsize = 20;
if (n < 0)
{
neg = 1;
n = -n;
}
buffer += bufsize;
*--buffer = '\0';
do
{
*--buffer = '0' + (n % 10);
n /= 10;
if (--d == 0)
{
d = 3;
*--buffer = ',';
}
}
while (n);
if (*buffer == ',') ++buffer;
if (neg) *--buffer = '-';
return buffer; …Run Code Online (Sandbox Code Playgroud) 当我使用itoa()它需要一个char*_DstBuff,这里最好的做法是什么?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num = 100;
// I'm sure here is no memory leak, but it needs to know the length.
char a[10];
// will this causue memory leak? if yes, how to avoid it?
// And why can itoa(num, b, 10); be excuted correctly since b
// has only allocated one char.
char *b = new char;
// What is the difference between char *c and char *b
// both …Run Code Online (Sandbox Code Playgroud) c++ ×3
c ×1
c# ×1
coding-style ×1
file-io ×1
input ×1
itoa ×1
java ×1
memory-leaks ×1
mono ×1
opengl ×1
opentk ×1
performance ×1
winapi ×1