小编use*_*154的帖子

从文本文件中读取行并将字符串放入向量中?

我试图读取文本文件的每一行,每行包含一个单词并将这些单词放入向量中.我该怎么做呢?

这是我的新代码:我认为它仍有问题.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    std::string line;
    vector<string> DataArray;
    vector<string> QueryArray;
    ifstream myfile("OHenry.txt");
    ifstream qfile("queries.txt");

    if(!myfile) //Always test the file open.
    {
        cout<<"Error opening output file"<<endl;
        system("pause");
        return -1;
    }
    while (std::getline(qfile, line))
    {
        QueryArray.push_back(line);
    }
    if(!qfile) //Always test the file open.
    {
        cout<<"Error opening output file"<<endl;
        system("pause");
        return -1;
    }

    while (std::getline(qfile, line))
    {
        QueryArray.push_back(line);
    }

    cout<<QueryArray[0]<<endl;
    cout<<DataArray[0]<<endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ fstream

23
推荐指数
3
解决办法
8万
查看次数

如何将类型向量<pair <char,int >>打印到c ++屏幕?

我有一个返回值向量的方法>我无法弄清楚如何打印此向量的内容.我试图循环内容,但我得到编译器错误.这是我尝试过的一个例子.

vector<pair<char, int>> output;

for(int i = 0; i < ouput.size; i++)
{
     cout << output[i][i] << endl; //output[i][i] does no work: no operator [] matches these operands
}
Run Code Online (Sandbox Code Playgroud)

c++ cout vector

11
推荐指数
2
解决办法
3万
查看次数

MIPS程序集用于简单的for循环

我需要将此C代码转换为MIPS程序集.这是C代码:

int tmp = 0; 
for (int  j = 0; j < 15; ++j) 
     tmp = tmp * 2 + 3
Run Code Online (Sandbox Code Playgroud)

这是我的MIPS汇编代码.这是正确的翻译吗?如果你发现任何错误,我真的很想知道.

# tmp = $v0
# j = $t0

.globl main

 main:
    li $v0,0

loop:
    bgt $t0,15,exit
    addi $t0,$t0,1
    mul $t1,$v0,2
    add $v0,$t1, 3
    j loop  

exit:
Run Code Online (Sandbox Code Playgroud)

assembly mips mips32

7
推荐指数
1
解决办法
7万
查看次数

如何计算哈希表中的冲突数?

我的插入函数已经正确处理了碰撞,但我希望能够计算每种不同散列方式(链接,线性探测和二次探测)中的碰撞次数.我该怎么做呢?

这是我的代码到目前为止:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <ctime>
#include "Chaining.h"
#include "QuadraticProbing.h"
#include "LinearProbing.h"

using namespace std;

int main()
{
    int collision_count = 0;
    float diff = 0.0;
    clock_t tStart, tStop;
    string ITEM_NOT_FOUND = "ITEM_NOT_FOUND";
    std::vector<std::string> DataArray;
    std::copy(std::istream_iterator<std::string>(std::ifstream("OHenry.txt")),istream_iterator<string>(),back_inserter(DataArray));
    std::vector<std::string> QueryArray;
    std::copy(std::istream_iterator<std::string>(std::ifstream("queries.txt")),istream_iterator<string>(),back_inserter(QueryArray));

    cout << "Testing chaining probing...\n";
    HashTable_chaining ChainingHT( ITEM_NOT_FOUND, 101 );
    int i = 0;
    double average_c = 0.0;
    double total_c = 0.0;
    double temp_c = 0.0;
    while(i != DataArray.size())
    {
        tStart = clock(); …
Run Code Online (Sandbox Code Playgroud)

c++ hash collision-detection

4
推荐指数
1
解决办法
4637
查看次数

j(jump)指令在内存中能跳转多远?(MIPS)

考虑 MIPS 中的 j(jump) 指令。它在内存中能跳多远?会是32位吗?我可以解释一下吗?

assembly mips mips32

4
推荐指数
1
解决办法
9659
查看次数

检查字符串是否为null时出现Null Exception错误?

当我运行我的代码时,我在文本框的不同行上放了几个字符串,但它打破说"Items.Add(item)"上有一个Null Exception Error我不知道为什么我收到此错误,因为在visual studio中的字符串in变量项不为null它包含一个返回字符,因此我不确定这是否是一个问题..例如item ="uno\r \n".此外,Items是一个字符串列表.有谁知道为什么我一直得到这个Null Exception?

    public List<string> Items;        


    public void getItemsFromTextBox(TextBox textbox)
    {
        string[] lines = textbox.Text.Split('\n');
        foreach (string item in lines)
        {
            if (!String.IsNullOrWhiteSpace(item))
                Items.Add(item);
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# textbox split

1
推荐指数
1
解决办法
205
查看次数

如何在C++标准库中启用哈希?

我尝试过使用#include<hash_map>#include <hash_set>我仍然得到相同的错误.

这是我的代码:

void HashTable_chaining::remove( const string & x )
{
    int hash_index = hash( x, theLists.size( ) ) ;

    list<string>&  whichList = theLists[ hash_index ];

    // search to make sure element not present
    for(list<string>::iterator itr=whichList.begin();itr!=whichList.end();itr++) {
        if(*itr==x) {
            theLists[hash_index].erase(itr);
            return;
        }
    }
    // element not found - so nothing to remove
}
Run Code Online (Sandbox Code Playgroud)

我的错误是:

Error   8   error C2872: 'hash' : ambiguous symbol  c:\users\aaron           johnson\desktop\program 5(johnson- noakes)\program 5(johnson- noakes)\chaining.cpp 32  1   Program 5(Johnson- Noakes)
Run Code Online (Sandbox Code Playgroud)

我有8个这样的错误.有什么建议?如何找出必须包含哪些标头才能使用哈希?

c++ hash stl hashtable

0
推荐指数
1
解决办法
1344
查看次数

在Ubuntu终端中编译时OpenGL引用的问题

我正在编译c ++文件和标题的目录.我认为我正确安装了openGL,Glut和Glew,但在运行时我一直在引用错误.

这是我正在做的以及编译器发回给我的错误:

user@Linux-machine:~/Documents/HW$ make
g++ -g framework.o poly_line.o shader_program.o circle.o controller.o main.o scene.o view.o -lGLEW -lglut -lGLU -o HW
framework.cpp:84: error: undefined reference to 'glGetError'
check_gl.h:30: error: undefined reference to 'glGetError'
check_gl.h:43: error: undefined reference to 'glGetError'
poly_line.cpp:23: error: undefined reference to 'glGenBuffers'
poly_line.cpp:28: error: undefined reference to 'glBindBuffer'
poly_line.cpp:29: error: undefined reference to 'glBufferData'
poly_line.cpp:54: error: undefined reference to 'glEnable'
poly_line.cpp:55: error: undefined reference to 'glEnable'
poly_line.cpp:56: error: undefined reference to 'glBlendFunc'
poly_line.cpp:57: error: undefined reference to 'glHint' …
Run Code Online (Sandbox Code Playgroud)

c++ opengl terminal glut glew

0
推荐指数
1
解决办法
6054
查看次数

我的随机数生成器在我的循环C#中没有创建新值

我正在使用c#尝试使用随机数填充数据网格视图但由于某种原因我在所有单元格中保持相同的值.

    public int GenerateRandomNumber()
    {
        const int minimumNumber = -9;
        const int maximumNumber = 15;
        var random = new Random();
        var randomNumber = random.Next(minimumNumber, maximumNumber);
        return randomNumber;
    }
Run Code Online (Sandbox Code Playgroud)

gameBoard是数据网格视图.

    private void populateButton_Click(object sender, EventArgs e)
    {
        CreateGameBoard((int)columnsNumericUpDown.Value,(int)rowsNumericUpDown.Value);
        gameBoard.RowTemplate.Height = gameBoard.Height/gameBoard.RowCount;
        foreach (DataGridViewRow row in gameBoard.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {

                cell.Value = GenerateRandomNumber();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# random

0
推荐指数
1
解决办法
475
查看次数

如何在LINQ Where语句中使用OR语句

基本上,我想查看给定的字符串是否包含在四个数据库条目之一中.我想要做的例子:

if (!String.IsNullOrEmpty(searchTagName))
{

   Articles = Articles.Where(b => b.Tag1.Contains(searchTagName));
   OR
   Articles = Articles.Where(b => b.Tag2.Contains(searchTagName));
   OR
   Articles = Articles.Where(b => b.Tag3.Contains(searchTagName));
   OR
   Articles = Articles.Where(b => b.Tag4.Contains(searchTagName));
}
Run Code Online (Sandbox Code Playgroud)

我该怎么写一个合适的

声明?

c# linq

0
推荐指数
1
解决办法
49
查看次数

标签 统计

c++ ×5

c# ×3

.net ×2

assembly ×2

hash ×2

mips ×2

mips32 ×2

collision-detection ×1

cout ×1

fstream ×1

glew ×1

glut ×1

hashtable ×1

linq ×1

opengl ×1

random ×1

split ×1

stl ×1

terminal ×1

textbox ×1

vector ×1