小编bal*_*lki的帖子

vector :: push_back()是一个浅层副本以及如何解决这个问题

在我写的程序中,我有类似于这里的代码:

#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

struct people
{
    string name;
    int st;
    int sn[50];
};

int main()
{
    unsigned int n,ST[10]={25,18,15,12,10,8,6,4,2,1};
    vector<people> master;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        unsigned int m;
        cin>>m;
        for (int j=0;j<m;j++)
        {
            people youngling; //I am declaring it here, but it doesn't solve the issue
            string s;
            cin>>s;
            for (int l=0;l<master.size();l++)
            {
                if (master[l].name.compare(s)==0)
                {
                    if (j<10) master[l].st+=ST[j];
                    master[l].sn[j]++;
                    goto loop;
                }
            }
            youngling.name=s;
            if (j<10) youngling.st=ST[j];
            for (int l=0;l<50;l++) youngling.sn[l]=0;
            youngling.sn[j]++;
            master.push_back(youngling);
            loop:; …
Run Code Online (Sandbox Code Playgroud)

c++ struct vector shallow-copy

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

如何在python中将一个列表的列表压缩成单个列表?

我有一个对象列表,其中对象可以是列表或标量.我想要一个只有标量的扁平列表.例如:

L = [35,53,[525,6743],64,63,[743,754,757]]
outputList = [35,53,525,6743,64,63,743,754,757]
Run Code Online (Sandbox Code Playgroud)

PS此问题的答案不适用于异构列表.在Python中展平浅层列表

python list flatten

4
推荐指数
3
解决办法
1630
查看次数

使用vimdiff查看多个文件对的差异

vimdiff显示两个或多个相同文件之间的差异.是否可以看到两对文件的差异.例如,当在.H文件和.C文件中查看差异时,可以方便地来回查看差异.一种方法是old.C new.C old.H new.H使用vim 打开然后拆分它们并:diffthis在每个缓冲区中运行.有没有更好的办法?

vim diff vimdiff

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

动态查询sqlalchemy中的列子集

假设表中只需要两列(name和id).我会编写如下代码:

session.query(User.id, User.name).all()
Run Code Online (Sandbox Code Playgroud)

但是如果列名是动态的,

def get_data(table, columns):
    return session.query(*(getattr(table, column) for column in columns)).all()
Run Code Online (Sandbox Code Playgroud)

但上面看起来很难看.有更好的推荐方式吗?

python orm sqlalchemy

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

如何在python中获取兄弟文件夹路径?

x, y, 和z是文件夹d:/

d:/x(当前的)

d:/y

d:/z

什么是得到的路径的最佳方式y,并z从给定的文件夹中x的文件夹路径。

python path python-3.x pathlib

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

golang 每分钟速率限制

如何限制每分钟 20 个请求?

import "golang.org/x/time/rate"

limiter := rate.NewLimiter(rate.Every(1*time.Minute), 20)

for {
    limiter.Wait()
    //more code
}
Run Code Online (Sandbox Code Playgroud)

这是行不通的。它的作用是,它允许前 20 个请求,然后每分钟只允许 1 个请求。预计第一分钟有 20 个请求(不需要像每 3 秒 1 个那样均匀分布),然后在第二分钟再有 20 个请求。在任意 1 分钟间隔内,发送的请求数不能超过 20 个。

我的解决方案: https: //stackoverflow.com/a/72452542

time go rate-limiting

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

何时使用alloca为类成员释放内存?

class MyString
{
  public:
  MyString(int length):_ptr(alloca(length))
  {
  }
  //Copy Constructor, destructor, other member functions.
  private:
  void* _ptr;
};

int main()
{
  MyString str(44);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它是在main函数的末尾释放还是在构造函数执行后立即释放?如果上面的代码按预期工作,那么有一个像这样的字符串类是一个好主意吗?

更新:

看起来主要的危险是

  1. 堆栈溢出
  2. 内联构造函数

我想我可以通过使用alloca来实现小尺寸和malloc/free来处理大尺寸的StackOverflow.我想必须有一些非可移植的编译器特定方式来强制编译器内联.

我感兴趣,因为字符串类是在任何c ++项目中广泛使用的东西.如果我做对了,我期望获得巨大的性能提升,因为大多数分配都会进入堆栈,否则会进入堆.这将是一个实用程序,最终用户将不会意识到内部.

c++ alloca initializer-list

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

boost Range库是下一个C++标准的一部分吗?

我试图决定是否可以在代码中开始使用基于范围的算法.如果它更有可能成为c ++标准的一部分,我可以在新代码中使用boost.虽然它看起来比迭代器好得多,但它可能使其他人难以审查并为项目做出贡献.我没有在这里的论文清单中找到.http://www.meetingcpp.com/

c++ boost c++11 boost-range c++14

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

如何在 asyncio 服务器中实现超时?

下面是一个简单的回显服务器。但是如果客户端在 10 秒内没有发送任何内容,我想关闭连接。

import asyncio


async def process(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
    print("awaiting for data")
    line = await reader.readline()
    print(f"received {line}")
    writer.write(line)
    print(f"sent {line}")
    await writer.drain()
    print(f"Drained")


async def timeout(task: asyncio.Task, duration):
    print("timeout started")
    await asyncio.sleep(duration)
    print("client unresponsive, cancelling")
    task.cancel()
    print("task cancelled")


async def new_session(reader, writer):
    print("new session started")
    task = asyncio.create_task(process(reader, writer))
    timer = asyncio.create_task(timeout(task, 10))
    await task
    print("task complete")
    timer.cancel()
    print("timer cancelled")
    writer.close()
    print("writer closed")


async def a_main():
    server = await asyncio.start_server(new_session, port=8088)
    await server.serve_forever()


if __name__ == '__main__': …
Run Code Online (Sandbox Code Playgroud)

python session-timeout python-3.x python-asyncio

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

异步版本运行速度比非异步版本慢

我的程序执行以下操作:

  1. 获取 .txt 文件的文件夹
  2. 对于每个文件:

    2.1. 读取文件

    2.2 将内容排序为列表并将列表推送到主列表

我在没有任何 async/await 的情况下这样做了,这些是时间统计信息

real    0m0.036s

user    0m0.018s

sys     0m0.009s
Run Code Online (Sandbox Code Playgroud)

使用下面的异步/等待代码我得到

real    0m0.144s

user    0m0.116s

sys     0m0.029s
Run Code Online (Sandbox Code Playgroud)

考虑到用例表明我错误地使用了 aysncio。

有人知道我做错了什么吗?

import asyncio
import aiofiles
import os

directory = "/tmp"
listOfLists = list()

async def sortingFiles(numbersInList):
    numbersInList.sort()

async def awaitProcessFiles(filename,numbersInList):
    await readFromFile(filename,numbersInList)
    await sortingFiles(numbersInList)
    await appendToList(numbersInList)


async def readFromFile(filename,numbersInList):
    async with aiofiles.open(directory+"/"+filename, 'r') as fin:
        async for line in fin:
            return numbersInList.append(int(line.strip("\n"),10))            
    fin.close()    

async def appendToList(numbersInList):
    listOfLists.append(numbersInList)

async def main():
    tasks=[]
    for filename …
Run Code Online (Sandbox Code Playgroud)

python asynchronous python-asyncio python-aiofiles

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