有没有办法在C++中获取一个给定大小的随机数,然后从文本文件中读取该行?无需逐步完成所有线路?我有这个只是逐行打印:
#include <cstdio>
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[]){
ifstream myReadFile;
myReadFile.open("words.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
printf("\n");
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用一些在框架上运行的遗留类型代码,因此我无法解释在较低级别上发生的事情,因为我不知道.
但是我的代码创建了一系列目标.
int maxSize = 20;
myObjects = new Object*[maxSize+1];
myObjects[0] = new item1(this);
myObjects[1] = new item2(this);
for(int i=2; i != maxSize+1; i++){
myObjects[i] = new item3(this);
}
myObjects[maxSize+1] = NULL;
Run Code Online (Sandbox Code Playgroud)
如果maxSize大于30,我会得到一大堆我从未见过的错误.Visual Studio在xutility突出显示中绘制错误:
const _Container_base12 *_Getcont() const
{ // get owning container
return (_Myproxy == 0 ? 0 : _Myproxy->_Mycont);
}
Run Code Online (Sandbox Code Playgroud)
我之前从未使用过Malloc,但这就是问题所在.我应该指定使用它来避免这个问题吗?
我应该知道这一点,但我不知道,我认为这可能是我基础知识方面的一个主要差距,所以我想我应该问专家.
鉴于:
char s1[] = { 'M', 'y', 'W', 'o', 'r', 'd' };
char s2[] = "MyWord";
cout << strlen(s1)<<endl;
cout << strlen(s2)<<endl;
cout << sizeof(s1)<<endl;
cout << sizeof(s2)<<endl;
Run Code Online (Sandbox Code Playgroud)
为什么当宣布为s1是strlen9,但申报时为s2是为6?额外的3来自哪里,它缺少一个空终止字符?
而且我明白sizeof(s2)比sizeof(s2)s2 大1个字节会自动添加空字符?
请温柔,TIA!
为什么这根据日食无效:
protected String doInBackground(String... arg0) {
publishProgress(10);
if(true){
//validate with DB
}else{
//send to registration screen
}
return null;
}
//Update Progress
protected void onProgressUpdate(Integer... values) {
mProgress.setProgress(values[0]);
}
Run Code Online (Sandbox Code Playgroud)
Eclipse这样说:
publishProgress(10); <<publishProgress(Void) is not acceptable for type publishProgress(Int)
Run Code Online (Sandbox Code Playgroud)
然而,这里的其他问题表明它以确切的方式使用.这是API的变化吗?我真的想传递一个字符串和int的publishProgress.这可能吗:
publishProgress("Starting Validation", 10);
Run Code Online (Sandbox Code Playgroud)
TIA
我有下表,它有一个跨越两行的中央图像保持单元:
<table width="100%" style="text-align:center">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td rowspan="2"><img src="http://www.skrenta.com/images/stackoverflow.jpg"/></td>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
<tr>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我希望表格和所有没有图像的单元格填充最大可用空间,但我希望图像单元格宽度调整为仅占用图像所需的空间。我认为这是您使用巧妙的边距值来实现效果的场景之一,但我无法弄清楚如何做到这一点。
TIA
我正在尝试将一些绘图功能集成到我的程序中.
我有一个JLabel,上面有一个图像集.
我想写一个方法来返回我的图像:
public Graphics getImage(){
Graphics g = currentImage;
return g
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将它从JLabel转换为图形对象.然后作为一个简单的测试我想在这张图片上画一条线:
public void paint(Graphics g) {
g.drawLine(20, 500, 700, 600);
}
Run Code Online (Sandbox Code Playgroud)
一些帮助开始这个将是伟大的.
我知道之前有人问过这个问题,我阅读了与之相关的线程,但那里的解决方案对我不起作用。
ifstream myFile;
myFile.open("largefile.txt");
if (myFile.is_open())
{
while (!myFile.eof( )) //step through each line until end of file
{
myFile>> str;
if(str.size() < 5){
amount++;
}
}
}
myFile.seekg(0, ios::beg);
if (myFile.is_open())
{
for(int i=0; i != random_integer; i++) //step through each line until random line reached
{
myFile>> str;
if(i == random_integer-1){
cout << "\n";
cout << str;
cout << "\n";
cout << str.size();
}
}
}
myFile.close();
Run Code Online (Sandbox Code Playgroud)
我读到在 while 语句中使用 EOF 是一个坏主意,还有什么选择?我如何在第二个循环中倒带,因为 seekg 不起作用,我不想关闭文件并再次打开,而且文件太大而无法真正读入数组?
TIA,我确定这是一个简单的解决方法,我对 C++ 还是很陌生。
我曾经想过一段时间,因为它似乎在我的经验丰富的代码中出现了很多.
我有一些代码使用了很多switch语句,但它真正做的就是每次都访问一个不同的队列.
void store(int toSwitchOn, float posx, float posy){
myDataStruct newValue;
newValue.psX = posx;
newValue.psY = posy;
switch(toSwitchOn){
case 1:
queue1.push(newValue);
break;
case 2:
queue2.push(newValue);
break;
case 3:
queue3.push(newValue);
break;
case 4:
queue4.push(newValue);
break;
case 5:
queue5.push(newValue);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
每个语句中唯一改变的是队列变量.是否有一些巧妙的方法来压缩这种重复的代码?
无论如何要避免做这样的事情:
std::queue<myStruct> part1, part2, part3, part4, part5, part6, part7, part8, part9, part10;
void setup(){
myVector.push_back(part1);
myVector.push_back(part2);
myVector.push_back(part3);
myVector.push_back(part4);
myVector.push_back(part5);
myVector.push_back(part6);
myVector.push_back(part7);
myVector.push_back(part8);
myVector.push_back(part9);
myVector.push_back(part10);
}
Run Code Online (Sandbox Code Playgroud)
虽然这只能达到第10部分,但我可能会达到50或更高.它只是编码的一部分,它必须在某个地方声明,或者是否有动态的方式我可以声明并将这些队列分配给myVector,而不是像这样编写它?
TIA
我有一个时间是由这样生成的:
SimpleDateFormat dateFormatUCT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormatUCT.setTimeZone(TimeZone.getTimeZone("UCT"));
String deadline = dateFormatUCT.format(new Date());
Run Code Online (Sandbox Code Playgroud)
我现在把它作为一个叫做截止日期的字符串.我希望稍后再次获取当前日期时间,并比较截止日期是否已过.如果没有通过,我想计算剩余的时间.有没有一个良好的内置方式这样做已经牢记数据现在是一个字符串?
TIA