小编jus*_*tik的帖子

Python:格式输出字符串,右对齐

我正在处理包含坐标x,y,z的文本文件

     1      128  1298039
123388        0        2
....
Run Code Online (Sandbox Code Playgroud)

每行使用分为3个项目

words = line.split()
Run Code Online (Sandbox Code Playgroud)

在处理数据之后,我需要在另一个txt文件中写回坐标,以便每列中的项目对齐(以及输入文件).每一行都由坐标组成

line_new = words[0]  + '  ' + words[1]  + '  ' words[2].
Run Code Online (Sandbox Code Playgroud)

std::setw()在C++中是否有类似的操纵器允许设置宽度和对齐?

python alignment string-formatting

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

C++ std :: transform of pairs-> first to new vector

对不起有点初学者的问题.有矢量和矢量对

typedef std::vector <int> TItems;
typedef std::vector < std::pair <int, int> > TPairs;
Run Code Online (Sandbox Code Playgroud)

有没有办法在一步中将对中的所有第一项转换为另一个向量

int main ()
{
TItems items;
TPairs pairs;

pairs.push_back (std::make_pair(1,3));
pairs.push_back (std::make_pair(5,7));

std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) );

return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何设计仿函数?

class comp
{
private:
     TPairs *pairs;

public:
    comp ( TPairs  *pairs_ ) : pairs ( pairs_) { }

    unsigned int operator () ( const unsigned int index ) const
    {
        return  (*pairs)[index].second != pairs->end();  //Bad idea
    }
};
Run Code Online (Sandbox Code Playgroud)

也许有一些没有lambda表达式和循环的用户友好方法.谢谢你的帮助.

c++ transform vector functor std-pair

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

点和椭圆(旋转)位置测试:算法

如何测试点P = [xp,yp]是否在由中心C = [x,y],a,b和phi(旋转角度)给出的某个旋转椭圆的内部/外部?

此时我正在使用以下解决方案:旋转椭圆并用角度-phi指向,然后对点的位置和"非旋转"椭圆进行常见测试.

但是有很多测试点(数千),我觉得这个解决方案很慢.有没有直接和更有效的方法来获得旋转椭圆和点的位置?

我不需要代码而是算法.谢谢你的帮助.

testing algorithm position point ellipse

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

Matlab,运算符A\B.

操作A\B的结果是什么,其中A(1,m)和B(1,m)?

在手册中写道:

A\B returns a least-squares solution to the system of equations A*x= B.
Run Code Online (Sandbox Code Playgroud)

所以它意味着x = inv(A'*A)*A'*B?但是,矩阵A'*A是单数的......

让我们假设:

A=[1 2 3]
B=[6 7 6]
A\B

0         0         0
0         0         0
2.0000    2.3333    2.0000
Run Code Online (Sandbox Code Playgroud)

如果使用MLS:

C = inv (A'*A)   singular matrix
C = pinv(A'*A)

0.0051    0.0102    0.0153
0.0102    0.0204    0.0306
0.0153    0.0306    0.0459

D= C*A'*B

0.4286    0.5000    0.4286
0.8571    1.0000    0.8571
1.2857    1.5000    1.2857
Run Code Online (Sandbox Code Playgroud)

所以结果A\B和inv(A'*A)*A'*B是不同的......

matlab operators

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

C++将内部结构作为参数传递

存在包含内部结构TIn的结构TOut:

template <typename T>
struct TOut
{
    struct TIn
    {
            bool b;
    };

    TIn in;
T t;
};
Run Code Online (Sandbox Code Playgroud)

如何正确传递TIn作为某种方法的形式参数?

class Test
{
public:
    template <typename T>
    static void test ( const TOut<T>::TIn &i) {} //Error
};


int main()
{
TOut <double> o;
Test::test(o.in);
}
Run Code Online (Sandbox Code Playgroud)

该程序编译时出现以下错误:

Error   4   error C2998: 'int test' : cannot be a template definition
Run Code Online (Sandbox Code Playgroud)

c++ templates struct parameter-passing inner-classes

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

Java:使用两种类型对通用类进行排序

假设以下具有2个类型T,U的泛型类

public class Pair<T, U> implements Comparable<T, U>  { //Error 1

   private final T first;
   private final U second;

   public Pair(T first_, U second_) {
      first = first_;
      second = second_;}

   public T getFirst() { return first; }
   public U getSecond() { return second; }
}
Run Code Online (Sandbox Code Playgroud)

及其项目清单

List<Pair<Integer, Integer>> = new ArrayList<>() 
Run Code Online (Sandbox Code Playgroud)

需要根据first / second属性进行排序。不幸的是,类定义包含一些问题,出现以下错误:

Error 1: wrong number of type arguments
Run Code Online (Sandbox Code Playgroud)

如何设计比较器类?此代码可能是完全错误的

public class SortBySecond implements Comparable <Pair <T, U>> {

    public int compare(final Pair<T, U> p1, final Pair<T, …
Run Code Online (Sandbox Code Playgroud)

java generics comparator

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

C++,快速从另一个向量唯一的向量中删除元素

有两个int的未分类向量和对int,int的向量

std::vector <int> v1;
std::vector <std::pair<int, float> > v2;
Run Code Online (Sandbox Code Playgroud)

包含数百万件物品.

如何尽可能快地从v1中删除这些v2.first独有的项目(即不包含在v2.first中)?

例:

v1:  5 3 2 4 7 8
v2: {2,8} {7,10} {5,0} {8,9}
----------------------------
v1: 3 4
Run Code Online (Sandbox Code Playgroud)

c++ vector unique

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

C++模板,vector.size在默认参数definitoin中使用

有结构TA

template <typename T>
struct TA
{
    typedef std::vector <T> Type;
};
Run Code Online (Sandbox Code Playgroud)

和test()函数具有类型TA的默认参数.

template <typename T>
void test ( typename TA<T>::Type a1, 
            typename TA<T>::Type a2 = typename TA<T>::Type(a1.size()) )
{}
Run Code Online (Sandbox Code Playgroud)

是否可以在默认参数a2定义中使用a1.size()?

int main()
{
    TA <double> ::Type a1;
    test<double>(a1);
}
Run Code Online (Sandbox Code Playgroud)

c++ parameters templates default definition

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

如何使迭代器指向与C++集中的另一个元素相同的元素?

对于相同类型的集合,有2个迭代器:

    typename TFunction <T>::Type ::const_iterator i1 = f1.begin();
    typename TFunction <T>::Type ::const_iterator i2 = f2.begin();
Run Code Online (Sandbox Code Playgroud)

在几个步骤之后,i1指向具有index = index1的f1的一些元素(可能不知道).我需要将第二个迭代器i2设置为具有与index1相同索引的f2元素...

这可以在没有将i1转换为索引的情况下完成吗?

c++ iterator set

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

C++,std :: list的左/右旋转

有没有办法如何使用std::rotate列表

std::list<int> v = { 0,7, 1,2 };
Run Code Online (Sandbox Code Playgroud)

因为这些左/右旋转

std::rotate(v.begin(), v.begin() + 1, v.end());
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
Run Code Online (Sandbox Code Playgroud)

为矢量工作?

std::vector<int> v = { 0, 7, 1, 2 };
Run Code Online (Sandbox Code Playgroud)

一种可能的方法是将列表复制到向量

std::vector<int> u{ std::begin(v), std::end(v) };
Run Code Online (Sandbox Code Playgroud)

反之亦然,但我发现它太"冗长"......直接轮换列表会导致以下错误:

Error   C2672   'std::rotate': no matching overloaded function found    
Error   C2676   binary '+':  std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

c++ algorithm list vector rotation

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