我从头开始创建一个新项目并编写用户商店来描述给定用户如何与系统交互.但是,我无法理解如何在没有第一个成为史诗的任务中打破第一个用户故事.
例如,如果我正在制造汽车,并且第一个用户故事说"作为司机,我希望能够改变运动的方向,这样我就不会碰到东西.",这意味着用户接口(方向盘),还有运动(车轮)和将它们连接在一起所需的一切(车轴,车架,连杆等......).最后,第一个用户故事似乎总是代表项目的大约40%,因为它对底层架构意味着很多.
你如何打破新项目的用户故事,使第一个不成为代表整个底层架构的史诗?
我有一个Visual Studio 2008 C#.NET 3.5项目,我希望有一个线程安全的Foo
对象池.
public class FooPool
{
private object pool_lock_ = new object();
private Dictionary<int, Foo> foo_pool_ = new Dictionary<int, Foo>();
// ...
public void Add(Foo f)
{
lock (pool_lock_)
{
foo_pool_.Add(SomeFooDescriminator, f);
}
}
public Foo this[string key]
{
get { return foo_pool_[key]; }
set { lock (pool_lock_) { foo_pool_[key] = value; } }
}
public IEnumerable<Foo> Foos
{
get
{
lock (pool_lock_)
{
// is this thread-safe?
return foo_pool_.Select(x => x.Value);
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C#.NET 3.5项目,其中一个类侦听来自另一个多线程类的事件调用.我需要确保我的事件只允许同时访问最多10个线程.第11个线程应该阻塞,直到10个完成之一.
myobj.SomeEvent += OnSomeEvent;
private void OnSomeEvent(object sender, MyEventArgs args)
{
// allow up to 10 threads simultaneous access. Block the 11th thread.
using (SomeThreadLock lock = new SomeThreadLock(10))
{
DoUsefulThings(args.foo);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法控制其他MyObj
类,所以我无法在那里实现线程池.
实现这个的最佳方法是什么?
谢谢,PaulH
我有一个Visual Studio 2008 C++项目,我想将该结构类型的向量的一个struct元素复制到一个新的向量.例如:
struct Foo {
int a;
long b;
};
std::vector< Foo > v1;
std::vector< long > v2;
for( std::vector< Foo >::const_iterator it = v1.begin(); it != v1.end(); ++it )
{
v2.push_back( it->b );
}
Run Code Online (Sandbox Code Playgroud)
有比这更好/更优雅的方式吗?
谢谢,PaulH
我有一个Visual Studio 2008 C++函数,我给出了一个以null结尾的字符串数组和该数组中字符串const char*
数的计数.
我正在寻找一种巧妙的方法来将数组转换const char*
为astd::vector< std::string >
/// @param count - number of strings in the array
/// @param array - array of null-terminated strings
/// @return - a vector of stl strings
std::vector< std::string > Convert( int count, const char* array[] );
Run Code Online (Sandbox Code Playgroud)
提升很好,STL很好.
谢谢,PaulH
我有一个C++类,我试图使用std :: bind1st将成员函数绑定到'this'参数.例如:
class MyClass
{
public:
void Foo()
{
using namespace std;
// this works fine
this->Bar();
// this also works fine
mem_fun( &MyClass::Bar )( this );
// this does not
bind1st( mem_fun( &MyClass::Bar ), this )();
// this is not a possibility for this program
boost::bind( &MyClass::Bar, this )();
};
void Bar()
{
};
};
Run Code Online (Sandbox Code Playgroud)
当我添加最后一个'bind1st'行时,我得到以下编译器错误:
1>stl/_function.h(189) : error C2039: 'second_argument_type' : is not a member of 'stlp_std::mem_fun_t<_Ret,_Tp>'
1> with
1> [
1> _Ret=void,
1> _Tp=MyClass
1> ] …
Run Code Online (Sandbox Code Playgroud) 我有一个适用于Windows Mobile 6.x的Visual Studio 2008 C++项目,我需要的内存比32MB进程插槽中的内存要多.所以,我正在寻找使用内存映射文件.我创建了一个标准的allocator实现,用CreateFileMapping和MapViewOfFile替换new/delete .
预期用途是这样的:
struct Foo
{
char a[ 1024 ];
};
int _tmain( int argc, _TCHAR* argv[] )
{
std::vector< boost::shared_ptr< Foo > > v;
for( int i = 0; i < 40000; ++i )
{
v.push_back( boost::allocate_shared< Foo >( MappedFileAllocator< Foo >() ) );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有了std::allocator
,我可以在例如28197次迭代之前,我得到一个std::bad_alloc
例外.有了MappedFileAllocator
,我在设备完全冻结之前得到了32371次迭代,并且必须重新启动.由于我的设备有512MB的RAM,我希望能够从该循环中获得更多的迭代.
我的MappedFileAllocator
实施是:
template< class T >
class MappedFileAllocator
{
public:
typedef T value_type;
typedef …
Run Code Online (Sandbox Code Playgroud) c++ memory windows-mobile virtual-memory memory-mapped-files
我遇到过C++ 03的一些采用这种形式的代码:
struct Foo {
int a;
int b;
CRITICAL_SECTION cs;
}
// DoFoo::Foo foo_;
void DoFoo::Foolish()
{
if( foo_.a == 4 )
{
PerformSomeTask();
EnterCriticalSection(&foo_.cs);
foo_.b = 7;
LeaveCriticalSection(&foo_.cs);
}
}
Run Code Online (Sandbox Code Playgroud)
读取是否foo_.a
需要保护?例如:
void DoFoo::Foolish()
{
EnterCriticalSection(&foo_.cs);
int a = foo_.a;
LeaveCriticalSection(&foo_.cs);
if( a == 4 )
{
PerformSomeTask();
EnterCriticalSection(&foo_.cs);
foo_.b = 7;
LeaveCriticalSection(&foo_.cs);
}
}
Run Code Online (Sandbox Code Playgroud)
如果是这样,为什么?
请假设整数是32位对齐的.该平台是ARM.
我有一个VS2008 C#.NET 3.5应用程序,我想在给定这些对象的IEnumerable列表的情况下创建对象的哈希表.
基本上,它看起来像这样:
public class MyCollection<T> : IEnumerable<T>
where T: IMyData, new()
{
private IDictionary<int, string> collection_ = new Dictionary<int, string>();
// ...
public void Add(T item)
{
collection_.Add(item.ID, item.Text);
}
public static MyCollection<T> Create(IEnumerable<T> source)
{
MyCollection<T> c = new MyCollection<T>();
foreach(T item in source)
{
c.Add(item);
}
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我想知道是否没有更好的方法从一个IEnumerable源复制到另一个.有什么建议?
谢谢,PaulH
我有一个ac#.net 3.5应用程序,它使用StreamWriter将文本写入控制台.有没有办法可以在打印到控制台的文本中添加下划线和删除线等文本装饰?可能使用ANSI转义序列?
TextWriter writer = new StreamWriter(Console.OpenStandardOutput());
writer.WriteLine("some underlined text");
Run Code Online (Sandbox Code Playgroud)
谢谢,PaulH
c++ ×5
c# ×4
stl ×3
.net-3.5 ×2
agile ×1
ansi ×1
collections ×1
concurrency ×1
console ×1
dictionary ×1
ienumerable ×1
mem-fun ×1
memory ×1
scrum ×1
string ×1
user-stories ×1
vector ×1
winapi ×1